2016-03-01 63 views
1

我承認,這確實是一個學校項目。這裏的問題本身,因爲我覺得它太複雜,無法轉述:查找用戶輸入的號碼連續有多少次。使用循環

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()); 



} 
} 

它不是最偉大的東西,我知道。任何幫助都將不勝感激。你自己有一個晚上。

+0

我可以看到你是初學者。你知道數組嗎?如果你這樣做,你應該首先創建一個大小爲「n」的int數組,並在繼續之前存儲用戶給出的int值。 – Maljam

+0

是的,我是初學者,不,我對陣列的方式知之甚少。恐怕我不知道如何執行你建議的有用代碼。 – Beatz

回答

0

在規範上停留一段時間,你已經圍繞用戶輸入一個密鑰,但是隻有在所有數字輸入正確後才能輸入密鑰?重新思考過程實際上需要重複,然後再詢問你是否會被卡住:)

+0

對不起,這實際上並不是一個答案我只是不能評論代表:/我是新哈哈是theres一些其他的方式來評論或我缺少的東西? – Hatward

+0

這不是說整個做什麼 - 而是聲明的目的?此外,還應該在問題下面以藍色「添加評論」。 – Beatz

+0

do while語句是在「現在輸入一個整數 - 鍵」,所以程序會不斷要求用戶輸入一個鍵,而任何== n.length(和我不能評論你的問題,直到我得到50代表) – Hatward

1

System.out.print("Now, enter "+n+" integer(s), of any kind: ");
int any = kbd.nextInt();

在這句話中,你不接受n個數。你只接受一個用戶號碼。數組是一種將多個相同數據類型的值一起存儲的方式。你可能想查找並閱讀關於java中的數組。

另外,您在do-while循環中接受來自用戶的密鑰。你只需要輸入一次密鑰。所以你可能想把代碼放在do-while循環之上。

最後,有一個新的變量,保持連續時間鍵的計數遇到。將其初始化爲零,現在循環嘗試跟蹤連續遇到密鑰的次數。

+0

嗯,我明白了。所以,我應該擺脫整個待辦事項聲明呢? – Beatz

+0

@Beatz它依賴於,如果你不使用數組,你必須使用循環來跟蹤用戶的輸入。 – Maljam

+0

讓我們一步一步來。首先將n個整數接受到一個數組中,看看它是否有效。然後在(1)中在你的do while循環中初始化2個新變量int count = 0和temp = 0(2),直到遇到與key,increment temp相同的元素。如果下一個元素不是鍵,則將temp和count的最大值保存在count中。讓我知道你是否需要更多幫助。 –

2

從您的老師和/或書籍的書面內容看,您需要使用數組或其中的一種形式來成功確定密鑰連續輸入的次數。以下是我會用數組做:

import java.util.Scanner; 
public class Consecutive 
{ 
    public static void main(String[] args) 
    { 
     // Variable declaration 
     int count = 0; 
     Scanner kbd = new Scanner(System.in); 

     System.out.print("Enter a positive integer: "); 
     int n = kbd.nextInt(); 

     System.out.print("Now, enter " + n + " integer(s), of any kind: ");  
     int[] ints = new int[n]; // Declares an array with length "n" to store the numbers 

     for (int i = 0; i < n; i++) 
     { 
      if (kbd.hasNextInt()) // Checks to make sure they are entering integer. 
       ints[i] = kbd.nextInt(); 
      else 
      { 
       i--; // Keeps increment the same unless you have a correct input 
       kbd.next(); // Clears the scanner so you can check the next thing entered. 
      } 
     } 

     System.out.println("Now enter one integer, the key: "); 
     int key = kbd.nextInt(); 

     for (int i = 0; i < ints.length; i++) 
     { 
      if (ints[i] == key) // Checks to see what value in array is equal to key 
       count++; // If it's equal add to count. 
     } 
     System.out.println("The key was entered " + count + " times."); // Display how many times key in array 
    } 
} 

要使用數組,你首先必須使用形式分配內存數組,:

type[] variableName = new type[optionalLength]; 

將值分配給數組,你只需輸入索引值(你可以想象一個數組中有一個值的槽),例如:

int[] oneTwoThree = new int[3]; 
oneTwoThree[0] = 1; 
oneTwoThree[1] = 2; 
oneTwoThree[2] = 3; 

請注意,在使用陣列時,它將從0開始,並轉到length - 1獲取索引值。在陣列申報價值的另一種方式是形式的東西:

int[] oneTwoThree = new int[3]; 
for (int i = 0; i < oneTwoThree.length; i++) 
    oneTwoThree[i] = i + 1; // i goes from 0 to 2, and we are setting oneTwoThree from one to three. 
+0

感謝您對代碼的評論以及答案。兩者都很有幫助。你有一個真棒夜! – Beatz

+0

你也是。我在我的答案中發佈了一些關於數組的更多信息以獲得更多解釋,如果我的答案對問題有幫助,那麼也可以將其標記爲解決問題的正確答案:) –

+0

謝謝,這確實有幫助。我的教授沒有像我期望的那樣徹底地完成數組。 – Beatz

0

一個好辦法來解決這些問題,當你第一次啓動編碼是寫一個算法爲「僞碼」。這意味着將它寫成英語種類,使您能夠專注於解決方案,而不必擔心語言語法。

例如,下面是一些psuedocode,用於計算列表中某個特定項目的最長連續序列。希望它會給你的東西上手

set longestChain to 0 
set currentChain to 0 
for all the items in the list 
    if item equals key 
     increment currentChain 
     if currentChain is greater than longestChain 
      set longestChain to currentChain 
    otherwise 
     set currentChain to 0 

本質上,它着眼於每個項目,以及它是否在關鍵遞增計數器。只要一個項目不等於該鍵,它就會將該計數器設置回零。同時它記錄了迄今爲止最長的系列。

讓我知道如果你有麻煩轉換爲代碼。

相關問題