2014-11-05 41 views
-1

我試圖創建一個簡單的20 questions game,通過採取用戶輸入採取用戶輸入,我相當新的編程java。用戶輸入和如果然後和其他

我已經爲我的問題設置了所有字符串,我想詢問用戶是否想玩。我試圖用用戶輸入設置if-then聲明,其中1的號碼是Yes,號碼2的號碼是No

我該如何設置?我試着用我的if(in.nextInt() = a)聲明,但我知道這是不對的。我知道我需要參考以前的用戶輸入,但我該怎麼做?感謝您提前提供的所有幫助。

import java.util.*; 

public class twentyq { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Scanner in = new Scanner(System.in); 

     int a = 1; 
     int b = 2; 

     // all the Strings needed for Questions[represented by the Q(1-20) variable] and their Answers[represented by the AQ(1-20) variable] 
     String Q1; 
     String Q2; 
     String Q3; 
     String Q4; 
     String Q5; 
     String Q6; 
     String Q7; 
     String Q8; 
     String Q9; 
     String Q10; 
     String Q11; 
     String Q12; 
     String Q13; 
     String Q14; 
     String Q15; 
     String Q16; 
     String Q17; 
     String Q18; 
     String Q19; 
     String Q20; 

     String AQ1; 
     String AQ2; 
     String AQ3; 
     String AQ4; 
     String AQ5; 
     String AQ6; 
     String AQ7; 
     String AQ8; 
     String AQ9; 
     String AQ10; 
     String AQ11; 
     String AQ12; 
     String AQ13; 
     String AQ14; 
     String AQ15; 
     String AQ16; 
     String AQ17; 
     String AQ18; 
     String AQ19; 
     String AQ20; 


     // The questions and their answers in numerical order question first then answer immediately following. 
     Q1 = "Where would you find the Sea of Tranquility?"; 
     AQ1 = "The Moon."; 

     Q2 = "What is the Capital of Spain"; 
     AQ2 = "Madrid."; 

     Q3 = "What is the painting, La Gioconda, more usually known as?"; 
     AQ3 = "The Mona Lisa."; 

     Q4 = "Which chess piece can only move diagonally?"; 
     AQ4 = "A Bishop."; 

     Q5 = "What is the oldest surviving printed book in the world?"; 
     AQ5 = "The Diamond Sutra, dated at 868 AD."; 

     Q6 = "Costing around $2,600 per pound, and made only to order by Knipschildt, what is the name of this chocolate truffle?"; 
     AQ6 = "Chocopologie"; 

     Q7 = "Who invented TV?"; 
     AQ7 = "George Carey, a Boston civil servant, first thought up television in 1876. John Logie Baird is often quoted as its inventor but his ideas didn't come along until the 1920's."; 

     Q8 = "What is allspice alternatively known as?"; 
     AQ8 = "Pimento."; 

     Q9 = "In publishing, what does POD mean?"; 
     AQ9 = "Print on demand."; 

     Q10 = "What is John Leach famous for making?"; 
     AQ10 = "Pottery."; 

     Q11 = "When was the euro introduced as legal currency on the world market?"; 
     AQ11 = "1st January, 1999."; 

     Q12 = "How many valves does a trumpet have?"; 
     AQ12 = "3."; 

     Q13 = "Which kind of bulbs were once exchanged as a form of currency?"; 
     AQ13 = "Tulips."; 

     Q14 = "Name the director of the Lord of the Rings trilogy.";  
     AQ14 = "Peter Jackson."; 

     Q15 = "Name the largest fresh water lake in the world?"; 
     AQ15 = "Lake Superior."; 

     Q16 = "Name the seventh planet from the sun."; 
     AQ16 = "Uranus."; 

     Q17 = "Which country is Prague in?"; 
     AQ17 = "Czech Republic."; 

     Q18 = "What is the oldest film ever made, and when was it made?"; 
     AQ18 = "Roundhay Garden Scene, made in 1888."; 

     Q19 = "Name the three primary colors."; 
     AQ19 = "Red, yellow and blue."; 

     Q20 = "How old is the world's oldest dictionary?"; 
     AQ20 = "Cuniform tablets with bilingual Sumerian-Akkadian word-lists have been dated to 2300 BC."; 

     System.out.println("Welcome To KCH39's 20 Questions!"); 
     System.out.println("Would you like to play? If yes, press 1 and enter. If not, press 2 and enter."); 

     in.nextInt(); 

     if (in.nextInt() = a){ 
      system.out.println(Q1); 


     } 


    } 

} 
+0

感謝編輯:)。沒有意識到我搞砸了。 – KCH39 2014-11-05 15:18:14

+5

您可能需要閱讀[數組](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/array.html)。 – 2014-11-05 15:18:17

+0

簡單可能是有一個類「問題」。然後在這裏有兩個字符串,「q」和「a」來表示問題和答案。然後將這些添加到數組問題[20]。這會減少很多重複。 – Skepi 2014-11-05 15:28:07

回答

2

您正在使用的賦值運算符,而不是相等操作符:

if (in.nextInt() == a){ 
    system.out.println(Q1); 
} 
+0

好的,讓我試試看。 – KCH39 2014-11-05 15:18:57

1

在你當前的代碼,改變(in.nextInt() = a)(in.nextInt() == a)

if(in.nextInt() == a){ 
    System.out.println(q1); 
    } 

=被賦值運算符和==(等號運算符)用於比較。

Source

+0

現在,當我按1並按下輸入它不會打印下一行。我怎麼能解決這個問題? – KCH39 2014-11-05 15:25:20

+0

將system.out.println()更改爲System.out.println()? – Haris 2014-11-05 15:26:34

0

使用等於運算符(==)而不是分配(=)....

in.nextInt(); 

     if (in.nextInt() == a){ 
      system.out.println(Q1); 


     } 

另一個選項是第一分配該值到另一個int和然後檢查

int b == in.nextInt() 

if(b==a) 
0

您的程序拋出以下編譯器錯誤。保證比較你試圖將'a'的值賦給in.nextInt(),這是不可能的。

twentyq.java:120: error: unexpected type 
    if (in.nextInt() = a){ 
       ^
    required: variable 
    found: value 
1 error 

所以它必須是固定如下(即使用雙等於):

if (in.nextInt() == a){ 
     System.out.println(Q1); 
    }