2017-07-28 36 views
-2

這是整個項目,它應該盡我所能地看到,但我希望在這裏張貼它可以讓我看到我錯過的東西,我有一個溫和的與Java經驗量Java代碼將不會打印出答案或正確讀取int

package com.company; 

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

public class Main { 

public static void main(String[] args) { 
// write your code here 
    Scanner input = new Scanner(System.in); 

    int number1; 
    int number2; 
    int answer; 
    String operator; 

此計算出的數字和運營商的用戶想要什麼

System.out.println("Please Enter your first number"); 
     number1 = input.nextInt(); 
     System.out.println("Please enter your second number"); 
     number2 = input.nextInt(); 
     System.out.println("Please enter your operator: + , - , * ,/"); 
      operator = input.next(); 

if語句應該確定用戶希望其子及applys兩個數字,然後調用randomEquation方法使其成爲錯誤答案

  if (operator == "+") { 
       answer = number1 + number2; 
       System.out.println("Your answer is: " + randomEquation(answer)); 
      } else if (operator == "-") { 
       answer = number1 - number2; 
       System.out.println("Your answer is: " + randomEquation(answer)); 
      } else if (operator == "*") { 
       answer = number1 * number2; 
       System.out.println("Your answer is: " + randomEquation(answer)); 
      } else if (operator == "/") { 
       answer = number1/number2; 
       System.out.println("Your answer is: " + randomEquation(answer)); 
      } 



} 

這種方法適用於隨機其中的一個答案創建錯誤的答案

public static int randomEquation(int number) { 
    Random rand = new Random(); 
    int random = rand.nextInt(100) + 1; 
    int answer = number; 
    if (random <= 100 && random >= 81) { 
     answer = number * 25; 
     return answer; 
    } 
    else if(random <= 80 && random >= 61){ 
     answer += 13; 
     return answer; 
    } 
    else if(random <= 60 && random >= 41){ 
     answer /= 2; 
     return answer; 
    } 
    else if(random <= 40 && random >= 21){ 
     answer -= 16; 
     return answer; 
    } 
    else{ 
     answer %= 4; 
     return answer; 
    } 
    } 
    } 

回答

1

您需要使用.equals與絃樂。

相反的:

operator == "+" 

嘗試:

"+".equals(operator)