java - 从java中的for语句中调用方法
问题描述
我仍然是一个相当新手的程序员,所以请原谅我乱七八糟的代码。当我尝试编译以下代码时,出现错误:
The method Customer(java.lang.String, java.lang.Double) is undefined for the type Customer
我之前曾尝试在 for 语句中声明对象“s”,然后它会在程序末尾创建一个新错误,在此我再次引用对象“s”。我想知道如何解决这个问题?下面我包含了我当前的程序,以及所需的输出和Customer.class API
.
import java.util.Scanner;
public class A4
{
public static void main( String[] args )
{
//delcaring variables
Scanner input = new Scanner( System.in );
String[] name = new String[5];
Double[] amount = new Double[5];
Customer s = new Customer( "", 0 );
//begining of program
for ( int i = 0; i <= 5; i++ )
{
System.out.println( "please enter the name of the customer" );
name[i] = input.next();
System.out.println( "please enter the amount in that account" );
amount[i] = input.nextDouble();
s.Customer( name[i], amount[i] );
//Customer s = new Customer(name [i],amount[i]);
}
System.out.println( "Search for all customers who have more than $100" );
for ( int t = 0; t <= 5; t++ )
{
if ( amount[t] >= 100 )
{
System.out.println( name[t] );
}
}
Double avgBalance = 0.0;
for ( int r = 1; r <= 5; r++ )
{
avgBalance += r;
}
avgBalance = avgBalance / 5;
System.out.println( "The average balance is: " + avgBalance );
Double max = amount[1];
for ( int j = 0; j <= 5; j++ )
{
if ( amount[j] > max )
{
max = amount[j];
}
}
System.out.println( "The customer with the highest balance is: " + max );
System.out.println( "Show all accounts after a 5% balance increase" );
//Customer c = new Customer(name [i],amount[i]);
for ( int e = 0; e <= 5; e++ )
{
//Customer c = new Customer amount[e].applyPercentageIncrease(5);
//amount [e]=
//applyPercentageIncrease q = new
s.applyPercentageIncrease( 5 );
System.out.println( s.getName() + " has " + s.getBalance() );
}
}
}
更新:我已经实现了 Gtomika 的建议并且程序现在运行,但是在第一个 for 语句中,程序询问用户 6 个名称然后设置一个越界错误,我理解越界错误,但是 for 语句条件声明它应该只要求 5 个名称和 5 个余额,对吗?我在这里错过了什么吗?
UPDATE2:我已经解决了上述问题并改进了我的程序,现在我需要一些帮助来使用 API 中的applyPercentageIncrease
方法Customer.class
。我知道我在下面编写的代码不起作用,但如果使用上述方法将所有账户余额增加 5%,我将不胜感激。谢谢
for (int e=0;e<5;e++)
{
//Customer c = new Customer amount[e].applyPercentageIncrease(5);
//amount [e]=
//applyPercentageIncrease q = new
customers[o]=e.applyPercentageIncrease(5);
System.out.println (o.getName()+" has "+o.getBalance());
}
解决方案
您还应该声明 Customer 对象的数组(或者可能是 ArrayList 或 LinkedList):
Customer[] customers = new Customer[5];
这样在 for 循环中你可以说:
for (int i=1;i<=5;i++)
{
System.out.println("please enter the name of the customer");
name[i] = input.next();
System.out.println("please enter the amount in that account");
amount[i] = input.nextDouble();
customers[i] = new Customer(name[i],amount[i]); //set the i. element of the Customer array
}
现在您可以在此循环之后引用创建的 Customer 对象!
推荐阅读
- javascript - 修复页面滚动百分比返回无限?
- performance - 在紧密循环中访问 OCaml 次要 GC 信息
- vb.net - 在共享日历中创建 AppointmentItems - 找不到对象异常
- r - r 将小数转换为漂亮的分数
- c# - 在构造函数中投影时无法访问导航属性
- c++ - 在 C++ 结构中,构造函数中的默认值和默认参数有什么区别?
- java - 如何使用 Java 访问安装在 Windows 上的驱动器?
- angular - 在执行下一个函数之前,如何等待来自 Angular 引导模式窗口的响应?
- google-bigquery - 如何用有序数字替换列中的字符串?
- php - 我需要在计算机上隐藏 plist 文件