2016-11-23 73 views
0

好的,所以我對編程非常新,我正在探索我迄今爲止所能做的。我寫了一個看起來像遊戲開始的代碼。我的目標是讓計算機向3個不同的用戶詢問他們的姓名,並要求他們輸入「滾動」一詞來分配一個隨機數字。然後,我希望計算機檢查哪個號碼是最高的,以查看誰將首先到達,等等。當我運行代碼時,代碼一直運行到它應該檢查哪個數字最高的地步。到目前爲止,我寫了這個:線程「main」java.lang.NumberFormatException中的異常:用於輸入字符串:「Roll」

import java.util.Scanner; 
import java.util.Random; 
class apples{ 
public static void main(String args[]){ 
Scanner name = new Scanner(System.in); 
Random order = new Random(); 
String n1; 
String n2; 
String n3; 
String a1; 
String a2; 
String a3; 
System.out.println("Enter the name of the first player: "); 
n1 = name.nextLine(); 
System.out.println("Enter the name of the second player: "); 
n2 = name.nextLine(); 
System.out.println("Enter the name of the third player: "); 
n3 = name.nextLine(); 
System.out.println(n1+ " type 'Roll' for your number: "); 
a1 = name.nextLine(); 
if(a1.equals("Roll")){ 
String player1 =String.valueOf(1+order.nextInt(10)); 
System.out.println(player1); 
}else{ 
System.out.println("Please type in Roll."); 
    } 
    System.out.println(n2+ " type 'Roll' for your number: "); 
    a2 = name.nextLine(); 
    if(a2.equals("Roll")){ 
    String player2 =String.valueOf(1+order.nextInt(10)); 
    System.out.println(player2); 
}else{ 
    System.out.println("Please type in Roll."); 
    } 
    System.out.println(n3+ " type 'Roll' for your number: "); 
    a3 = name.nextLine(); 
    if(a3.equals("Roll")){ 
    String player3 =String.valueOf(1+order.nextInt(10)); 
    System.out.println(player3); 
    }else{ 
    System.out.println("Please type in Roll."); 
    } 

int num1 = Integer.parseInt(a1); 
int num2 = Integer.parseInt(a2); 
int num3 = Integer.parseInt(a3); 
    if(num1>num2 && num1>num3){ 
    System.out.println(n1+" You will go first."); 
    }else{ 
    if(num2>num1 && num2>num3){ 
     System.out.println(n2+" You will go first."); 
    }else{ 
     if(num3>num1 && num3>num2){ 
     System.out.println(n3+" You will go first"); 
     } 
    } 
    } 
} 

我收到錯誤消息是這樣的:

Exception in thread "main" java.lang.NumberFormatException: For input  string: "Roll" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at apples.main(apples.java:44) 

我試圖改變字符串A1,A2,A3,以PLAYER1,player2,player3看看如果這有效,但它說,player1不能解析爲變量。 感謝您提前提供任何幫助!

+1

請格式化您的代碼。現在幾乎不可能遵循。 –

+0

基本上,你只是想把「Roll」轉換成一個整數...... –

+0

「int num1 = Integer.parseInt(a1)」是將一個String解析成一個Int。它不喜歡這樣。也許從3分配他們的隨機數?然後,它會隨機挑選訂單。 ()* 4 int num2 = Math.random()* 4 int num3 = Math.random()* 4 –

回答

0

你的問題是由以下順序觸發:

int num1 = Integer.parseInt(a1); 
int num2 = Integer.parseInt(a2); 
int num3 = Integer.parseInt(a3); 

您正在嘗試將字符串轉換爲一個整數,心不是可能的,除非你的字符串只包含數字。

在你的情況,你的字符串是這樣的:

a1 = "Roll" 
a2 = "Roll" 
a3 = "Roll" 

我認爲這你的arent要處理的正確的價值觀。您是否希望您通過獲取輸入工作:

player1 = String.valueOf(1 + order.nextInt(10)); 


import java.util.Scanner; 
import java.util.Random; 

class apples { 

    public static void main(String args[]) { 
     Scanner name = new Scanner(System.in); 
     Random order = new Random(); 
     String player1 = null; 
     String player2 = null; 
     String player3 = null; 
     System.out.println("Enter the name of the first player: "); 
     String n1 = name.nextLine(); 
     System.out.println("Enter the name of the second player: "); 
     String n2 = name.nextLine(); 
     System.out.println("Enter the name of the third player: "); 
     String n3 = name.nextLine(); 
     System.out.println(n1 + " type 'Roll' for your number: "); 
     String a1 = name.nextLine(); 
     if (a1.equals("Roll")) { 
      player1 = String.valueOf(1 + order.nextInt(10)); 
      System.out.println(player1); 
     } else { 
      System.out.println("Please type in Roll."); 
      player1 = "0"; 
     } 
     System.out.println(n2 + " type 'Roll' for your number: "); 
     String a2 = name.nextLine(); 
     if (a2.equals("Roll")) { 
      player2 = String.valueOf(1 + order.nextInt(10)); 
      System.out.println(player2); 
     } else { 
      System.out.println("Please type in Roll."); 
      player2 = "0"; 
     } 
     System.out.println(n3 + " type 'Roll' for your number: "); 
     String a3 = name.nextLine(); 
     if (a3.equals("Roll")) { 
      player3 = String.valueOf(1 + order.nextInt(10)); 
      System.out.println(player3); 
     } else { 
      System.out.println("Please type in Roll."); 
      player3 = "0"; 
     } 
     int num1 = Integer.parseInt(player1); 
     int num2 = Integer.parseInt(player2); 
     int num3 = Integer.parseInt(player3); 
     if (num1 > num2 && num1 > num3) { 
      System.out.println(n1 + " You will go first."); 
     } else { 
      if (num2 > num1 && num2 > num3) { 
       System.out.println(n2 + " You will go first."); 
      } else { 
       if (num3 > num1 && num3 > num2) { 
        System.out.println(n3 + " You will go first"); 
       } 
      } 
     } 
    } 
} 

只是一個小提示:爲了避免從字符串到整數轉換您生成的數字,投下他們作爲整數直接,如:

int numberPlayerOne; 
....... 
....... 
numberPlayerOne = 1 + order.nextInt(10); 

如果要將生成的數字直接解析爲整數,請使用以下代碼:

import java.util.Scanner; 
import java.util.Random; 

class apples { 

    public static void main(String args[]) { 
     Scanner name = new Scanner(System.in); 
     Random order = new Random(); 
     int numberPlayerOne = 0; 
     int numberPlayerTwo = 0; 
     int numberPlayerThree = 0; 
     System.out.println("Enter the name of the first player: "); 
     String n1 = name.nextLine(); 
     System.out.println("Enter the name of the second player: "); 
     String n2 = name.nextLine(); 
     System.out.println("Enter the name of the third player: "); 
     String n3 = name.nextLine(); 
     System.out.println(n1 + " type 'Roll' for your number: "); 
     String a1 = name.nextLine(); 
     if (a1.equals("Roll")) { 
      numberPlayerOne = 1 + order.nextInt(10); 
      System.out.println(numberPlayerOne); 
     } else { 
      System.out.println("Please type in Roll."); 
     } 
     System.out.println(n2 + " type 'Roll' for your number: "); 
     String a2 = name.nextLine(); 
     if (a2.equals("Roll")) { 
      numberPlayerTwo = 1 + order.nextInt(10); 
      System.out.println(numberPlayerTwo); 
     } else { 
      System.out.println("Please type in Roll."); 
     } 
     System.out.println(n3 + " type 'Roll' for your number: "); 
     String a3 = name.nextLine(); 
     if (a3.equals("Roll")) { 
      numberPlayerThree = 1 + order.nextInt(10); 
      System.out.println(numberPlayerThree); 
     } else { 
      System.out.println("Please type in Roll."); 
     } 
     if (numberPlayerOne > numberPlayerTwo && numberPlayerOne > numberPlayerThree) { 
      System.out.println(n1 + " You will go first."); 
     } else { 
      if (numberPlayerTwo > numberPlayerOne && numberPlayerTwo > numberPlayerThree) { 
       System.out.println(n2 + " You will go first."); 
      } else { 
       if (numberPlayerThree > numberPlayerOne && numberPlayerThree > numberPlayerTwo) { 
        System.out.println(n3 + " You will go first"); 
       } 
      } 
     } 
    } 

} 
相關問題