vendredi 28 juillet 2017

Java code will not print out answer or read int properly

This is the whole project, it should work as far as I can see but i'm hoping that posting it here could let me see something I missed, I have a moderate amount of experience with java

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;

This figures out what numbers and operators the user wants

 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();

The if Statement should determine which operator the user wants and applys it to the two numbers and then calls the randomEquation Method to make it the wrong answer

           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));
            }



}

This method randomly applies one of these to the answer to create the wrong 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;
    }
   }
  }

Aucun commentaire:

Enregistrer un commentaire