我仍然是一个不错的新手程序员,所以请原谅我的混乱代码。当我尝试编译以下代码时,出现错误:
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个名称,然后设置一个超出范围的错误,我知道超出范围的错误,但是对于语句条件,它只要求输入5个名称和5个余额,对吗?我在这里想念东西吗?
UPDATE2:我已经解决了上述问题并改进了程序,现在在使用applyPercentageIncrease
API中的方法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());
}
答案 0 :(得分:0)
您还应该声明和排列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对象!