嘿,我正在做一個數字平均程序,我想插入這個函數,允許用戶輸入字母「y」再次運行並做另一個計算,但是,我的程序顯示終止的消息(我'第一次計算之後,即使我希望用戶能夠輸入輸入,也可以使用Eclipse)。 這裏是困擾我的源代碼的一部分:我的程序不應該終止。爲什麼?
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("This is an averaging program. How many numbers would you like to average together?");
int total=input.nextInt();
int i;
float sum=0;
for(i=0; i<total; i++)
{
System.out.print("Enter your numbers: ");
sum += input.nextFloat();
}
String y;
System.out.print("Your average is: " + sum/total + "\n");
System.out.print("Would you like to do another computation? Type y for yes, or something else for no.");
y=input.nextLine();
好吧,如果這是程序的結束,那麼沒有任何事情可以讓你在回答提示後重新開始。 – 2013-02-14 02:48:28
這不是程序的結尾,它是其中的一部分。我寫它使得main()表示如果用戶輸入「y」,則另一個相同的函數開始運行,並且如果用戶想要輸入另一個計算,那麼該函數會自行調用它。 – 2013-02-14 02:54:58
啊,所以有兩件事:1)這種重複是可怕的。不要這樣做。 2)[user000001的答案](http://stackoverflow.com/a/14866999/839646)可能是你在找什麼。 – 2013-02-14 02:56:38