我承認,這確實是一個學校項目。這裏的問題本身,因爲我覺得它太複雜,無法轉述:查找用戶輸入的號碼連續有多少次。使用循環
Write a JAVA program that prompts the user to enter a positive integer and store it in a
variable named n.
You may assume without error checking that the value n is positive.
The user is then prompted to enter n number of integers. After the n integers have been
entered, the program then prompts the user to enter another single integer to be stored in
a variable named key. The program must now determine and output the maximum number of
times the value key was entered consecutively. (If the key was never entered, output 0. If
it was entered, but never twice in a row, output 1.)
對不起文本牆,但是你有它。現在,我不求答案。我有一些我一直在努力的代碼,但我無法弄清楚連續的部分。這是它:
import java.util.Scanner;
public class Consecutive {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
String n = kbd.nextLine();
System.out.print("Now, enter "+n+" integer(s), of any kind: ");
int any = kbd.nextInt();
do {
System.out.println("Now enter one integer, the key: ");
int key = kbd.nextInt();
for (int i = 0; i < n.length(); i++) {
} // Havent figured out this part yet, or if its even needed.
} while (any == n.length());
}
}
它不是最偉大的東西,我知道。任何幫助都將不勝感激。你自己有一個晚上。
我可以看到你是初學者。你知道數組嗎?如果你這樣做,你應該首先創建一個大小爲「n」的int數組,並在繼續之前存儲用戶給出的int值。 – Maljam
是的,我是初學者,不,我對陣列的方式知之甚少。恐怕我不知道如何執行你建議的有用代碼。 – Beatz