2017-06-03 25 views
0

遇到從數組索引爲隨機語句輸出輸出字符串的問題。哈希映射很好地捕獲了數組中每個字符串的數量,但是一旦它具有所有信息都會給出這些項目的int顯示。目標是讓用戶在選擇主題 - 動詞 - 形容詞 - 賓語 - 副詞之後選擇表單項,然後進行隨機輸出。 也出於某種原因,當它在命名前遇到了用戶輸入信息時,它會碰到副詞。在從數組索引中爲隨機輸出放置字符串時出現問題

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package shoutbox; 
import java.util.HashMap; 
import java.util.Random; 
import java.util.Scanner; 
/** 
* 
* @author alden 
*/ 
public class Shoutbox { 

    /** 
    * 
    */ 
    public int t; 
    public int o; 
    public int r; 
    public int q; 
    public int p; 
public String subjectArray[]=new String[t]; // Intilized array by the size of int y 
public String verbArray[]=new String[o]; 
public String adjectiveArray[]=new String[r]; 
public String objectArray[]=new String[q]; 
public String adverbArray[]=new String[p]; 

public Shoutbox(){ 


} 


    public static void main(String[] args) { 
     // TODO code application logic here 
     Boolean repeat = true; 
     System.out.println(" Please select a option"); 

     HashMap <Integer, String> map = new HashMap<>(); // starts the hashmap 


     map.put(0,"Are you new to programming"); 
     map.put(1, "I think java is a good start"); 
     map.put(2, "It is easy to learn"); 
     map.put(3, "You tube is a great resource"); 
     map.put(4,"Most people start with hello world"); 
     map.put(5,"After this you learn Scanners and getter and setters"); 
     map.put(6,"Next is encapsulation"); 
     map.put(7,"After this is loops"); 
     map.put(8,"Once you get loops work on arrays"); 
     map.put(9,"Next you should look at objects"); 


     map.entrySet().forEach((entry) -> { 
      // for loop for the display of has map 
      int key = entry.getKey(); 
      String value = entry.getValue(); 
      System.out.println(key + " : " + value); 
     }); 
char y; 
while (repeat = true){ // gives options for y or n 
System.out.println("Please enter a value"); 
Scanner in = new Scanner(System.in); 
int n = in.nextInt(); 
String s = map.get(n); 
System.out.println(s); 

System.out.println("Would you like to enter another value. y/n"); 
y = in.next().charAt(0); 

      switch (y) { 
       case 'y': 
        repeat = true; 
        break; 
       case 'Y': 
        repeat = true; 
        break; 
       default: 
        repeat = false; 
        break; 
      } 
break; 

} 
String input1; 

boolean loop = false; 
Scanner in = new Scanner(System.in); 

System.out.printf("How many subjects are there?"); 
int t = in.nextInt(); 
String subjectArray[]=new String[t]; 

System.out.printf("How many verbs are there?"); 
int o = in.nextInt(); 
String verbArray[]=new String[o]; 

System.out.printf("How many adjective are there?"); 
int q = in.nextInt(); 
String adjectiveArray[]=new String[q]; 

System.out.printf("How many adjective are there?"); 
int r = in.nextInt(); 
String objectArray[]=new String[r]; 

System.out.printf("How many adverbs are there?"); 
int p = in.nextInt(); 
String adverbArray[]=new String[p]; 

    Scanner input = new Scanner(System.in); // aanother scanner for more input 

for(int subjectCount=0;subjectCount<t;subjectCount++){ 
subjectArray[subjectCount]= input.next(); 
    int tempSub= subjectCount+1; 
System.out.printf("Subject "+tempSub); 
// Assigned users value to array 
} 
int subjectIndex; 
Random randt = new Random(); 
subjectIndex = randt.nextInt(+1);//for loop 
// random word 
//Stores and finds random subject 
//System.out.println(subjectArray[subjectIndex]); 

for(int verbCount=0;verbCount<o;verbCount++){ 
verbArray[verbCount]= input.next(); // Assigned users value to array 
    int tempVerb= verbCount+1; 
    System.out.printf("verb "+tempVerb); 
    } 
int verbIndex; 
Random rando = new Random(); 
verbIndex = rando.nextInt(o); // random word 
// 


for(int adjectiveCount=0;adjectiveCount<r;adjectiveCount++){ 
adjectiveArray[adjectiveCount]= input.next(); // Assigned users value to array 
    int tempAdj= adjectiveCount+1; 
    System.out.printf("Adjective " + tempAdj); 
} 
int adjectiveIndex; 
Random randq = new Random(); 
adjectiveIndex = randq.nextInt(r); // random word 
// 

for(int objectCount=0;objectCount<q;objectCount++){ 
objectArray[objectCount]= input.next(); // Assigned users value to array 
    int tempObj= objectCount+1; 
    System.out.printf("Object "+ tempObj); 
} 
int objectIndex; 
Random randr = new Random(); 
objectIndex = randr.nextInt(q); // random word 
// 

for(int adverbCount=0;adverbCount<p;adverbCount++){ 
adverbArray[adverbCount]= input.next(); // Assigned users value to array 
    int tempAd= adverbCount+1; 
    System.out.printf("Adverb "+tempAd); 
} 
int adverbIndex; 
Random randp = new Random(); 
adverbIndex = randp.nextInt(p); 

input1 = (subjectIndex + " " + verbIndex +" "+ adjectiveIndex +" "+ objectIndex +" "+ adverbIndex); // adds all items togather for loop 
while(loop = true){ 
System.out.println(input1); 
break; 
} 

}} 
+0

不要在所有地方製造新的掃描儀。在程序的頂部製作一個'Scanner',並在需要輸入的地方使用它。這應該解決您的一些I/O問題。 –

+0

我改變它運行得稍微快一點,仍然試圖從數組中收集字符串輸出一個隨機語句是否有更好的方法來做到這一點,然後我在做什麼? –

回答

0

問題:

在幾個你寫while (something = true) {... break; }地方。顯然,你的意思是讓循環運行到something等於true,但是你卻發現循環是永遠運行的,所以在最後加上了break;的快速修復。

問題是,=轉讓;平等測試是==。你就OK了,如果你寫了:

while (something == true) { ... } 

你也可以寫在較短的版本

while (something) { ... } 

因爲somethingboolean,因此已經擁有了自己truefalse價值的價值,您無需針對true(或false)進行測試,以獲取您的循環需要的boolean條件。編寫something == truesomething == false實際上並不是錯誤的,只是比必要的更冗長。

建議:

您在隨機挑選的話,形成一個句子的基本邏輯基本上是好的,但它需要從用戶獲得這些詞列表的過程中解開,特別是如果你想使用最後一個循環來生成多個隨機句子。

獲得了不同詞類的隨機索引後,您需要從詞列表中提取相應的詞以實際構建句子;現在你只是打印出由索引本身組成的「句子」。

您不需要多個Random對象;它不是特別有害,只是額外的不必要的代碼行和幾個浪費的CPU週期。