我正试图编写一个程序,当500英镑的目标满足时需要停止。我必须使用DO WHILE循环来完成此操作。做while while循环时,更新最大捐助者名称
它需要记录它在达到500英镑之前收到的捐款数量,还需要记录捐赠人捐款最多和捐赠最多的人的姓名。
我无法让程序更新最高捐赠人的姓名。我到目前为止的代码如下。请告诉我我哪里错了。
我有一条红线来了下“namemax”当我尝试在循环外底打电话了,他说“没有初始化”
enter codeimport java.util.Scanner;
/**
* Created by IntelliJ IDEA.
* Date: 11/02/2015
* Time: 15:45
* UPDATE COMMENT ABOUT PROGRAM HERE
*/
public class DoWhile
{
public static void main(String[] args)
{
Scanner keyboard= new Scanner(System.in);
final double TOTAL=500;
String name,namemax;
double donation, donationTotal=0,currentMax=0;
int howManyDonation=0;
do
{
System.out.println("Please enter your name below");
name = keyboard.next();
System.out.println("");
System.out.println("Please enter the amount you would like to donate below");
donation = keyboard.nextDouble();
howManyDonation++;
donationTotal = donationTotal+donation;
if(donation>currentMax)
{
currentMax=donation;
namemax=name;
}//if
}//doWhile
while(donationTotal!=TOTAL);
System.out.println("The total number of donations is " + howManyDonation);
System.out.println("The largest donation was " + currentMax);
System.out.println("The name of the person with the largest donation is " + namemax);
}//main
}//class
here
namemax变量未初始化检查http://ideone.com/iF1lZ9 – silentprogrammer 2015-02-11 17:25:52
如果解决了问题,请接受其中一个答案。 – 2015-02-11 17:53:48