2014-01-21 86 views
0

我有兩個問題:我的代碼將不會運行,但沒有錯誤

  1. 爲什麼我的程序運行?
  2. 我該如何檢查輸入是否與某些 有什麼不同?

-

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

public class HeigherOrLower { 

     //Creates the scanner. 
    static Scanner sc = new Scanner(System.in); 
    static String input = sc.nextLine(); 

    public static void main(String[] args) 
    { 
     //Creating the two random numbers. 
     Random rand = new Random(); 
     int userNumber = rand.nextInt(10) + 1; 
     int comNumber = rand.nextInt(10) + 1; 

     System.out.println("Your number is: " + userNumber +" of 10"); 
     System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number"); 

      //Checking if the user is right. 
     if(userNumber < comNumber && input.equals("L")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 

     if(userNumber == comNumber && input.equals("E")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 

     if(userNumber > comNumber && input.equals("H")) 
       System.out.println("You are right. The computer's number is: " + comNumber); 
    } 
} 
+0

你如何運行它? –

+11

你認爲'static String input = sc.nextLine();'是做什麼的? –

+0

調試你的代碼,你會發現問題比提問更快。 – Salah

回答

-1

您可以檢查您是否已經命名自己的文件作爲HeigherOrLower.java否則它相應重命名,謝謝!

+0

這是一個瘋狂的猜測。是什麼讓你覺得這可能是問題? – Keppil

0

它將運行在

static String input = sc.nextLine(); 

等待用戶輸入,我認爲你不能當你在Eclipse中運行它,觀察控制檯。查看進程列表中是否存在java進程

0

它運行良好,但它在輸出任何文本之前等待用戶輸入。在「您認爲...」行之後,您可能需要將兩條靜態行移動到主方法中:

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


public class HeigherOrLower 
{ 

    public static void main(String[] args) 
    { 
     //Creating the two random numbers. 
     Random rand = new Random(); 
     int userNumber = rand.nextInt(10) + 1; 
     int comNumber = rand.nextInt(10) + 1; 

     System.out.println("Your number is: " + userNumber +" of 10"); 
     System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number"); 

     //Creates the scanner. 
     Scanner sc = new Scanner(System.in); 
     String input = sc.nextLine(); 

     //Checking if the user is right. 
     if(userNumber < comNumber && input.equals("L")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 

     if(userNumber == comNumber && input.equals("E")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 

     if(userNumber > comNumber && input.equals("H")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 
    } 
} 

靜態初始化程序在main方法之前執行。所以,在輸出任何東西之前,它都在等待輸入,給人的印象是什麼都沒有發生。

要檢查字符串值是否不相等,請使用非運算符「!」如下所示:

if (! input.equals("H")) 

對於數值,只要使用=如下所示:

if (x != 3) 
0

你的程序做「RUN」那只是你沒有任何prmpots == >> System.out.println(「請輸入您的輸入」)///或任何其他類型的提示消息來通知用戶。至於你的掃描儀,我刪除了無效修飾符「靜態」,並轉移到您的主類。至於邏輯,我不知道它應該做什麼。

 public class HeigherOrLower { 

    //Creates the scanner. 


    public static void main(String[] args) 
    { 

     System.out.println("Please type your input"); 
     Scanner sc = new Scanner(System.in); 
     String input = sc.nextLine(); 
    //Creating the two random numbers. 
    Random rand = new Random(); 
    int userNumber = rand.nextInt(10) + 1; 
    int comNumber = rand.nextInt(10) + 1; 

    System.out.println("Your number is: " + userNumber +" of 10"); 
    System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number"); 

     //Checking if the user is right. 
    if(userNumber < comNumber && input.equals("L")) 
     System.out.println("You are right. The computer's number is: " + comNumber); 

    if(userNumber == comNumber && input.equals("E")) 
     System.out.println("You are right. The computer's number is: " + comNumber); 

    if(userNumber > comNumber && input.equals("H")) 
      System.out.println("You are right. The computer's number is: " + comNumber); 




     } 
    } 
0

這兩條線:

static Scanner sc = new Scanner(System.in); 
static String input = sc.nextLine(); 

執行以下操作:

  • 構造使用標準輸入一個新的Scanner
  • Scanner
閱讀第一線

由於這不是在一個方法內部聲明的,所以當你運行該程序時,這種情況非常正確(我不會將你與技術問題混淆)。

因此,它在等待輸出任何內容之前讀取輸入。

所以你也許想要把那兩個語句,與static刪除,main方法裏面,這行之後可能:

System.out.println("Do you think that your number is Heigher(H), Lower(L) or Equal To(E) the computers number"); 

如果這不是問題,你可能確實不運行它完全可以,你必須提供更多關於你如何運行它的細節。

要檢查您輸入不等於什麼東西,你可以只說!input.equals("something")(if語句裏面,!這裏的意思是「不」),或者您可以使用else

if (input.equals("something")) 
    System.out.println("It's equal"); 
else 
    System.out.println("It's not equal"); 
相關問題