0

I wanted to program a little Program that generates an addition task with 4 random numbers with measurements, like 45mm+34dm+ and so on.... The second function is: when the user enters the right solution, in mm measurements in the console, the program should print out: "right". But on the second function lies the problem. Something doesn't work on the if statement I wrote for this function.

Here is the Code:

package Uebungen;
import java.util.Scanner;
import sun.applet.Main;
import java.util.Random;

public class AvuG {

    // Programmstart
    public static void main(String[] args) {
        // Declarationen
        Scanner scn= new Scanner(System.in);
        Random rG = new Random();
        int[] numbers = new int[4];
        int[] measurments = new int[4];

        //Fills Array with four Random numbers for future measurment generation
        for (int i = 0; i < maßEinheiten.length; i++) {
            measurments[i] = rG.nextInt(4);
        }

        // Fills Array with four random numbers
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = rG.nextInt(99);
        }

        // Prints out 4 numbers with measurments
        for (int i = 0; i < numbers.length; i++) {
            if (i == numbers.length - 1) {
                System.out.print(data[i] + checkInput(measurments[i]));
            } else {
                System.out.print(data[i] + checkInput(measurments[i]) + " + ");
            }
        }

        //Calculates Solution of the calculation in measure mm in the Background for future use. 
        for (int i = 0; i < measurments.length; i++) {
            switch(measurments[i]) {
            case 1:
                result += numbers[i];
                break;
            case 2:
                result += numbers[i] * 10;
                break;
            case 3:
                result += numbers[i] * 1000;
                break;
            case 0:
                result += numbers[i] * 100;
                break;
            }
        }

        // Solution of the calculation, so you dont have to calculate when you want to investigate if its working.
        System.out.println("");
        System.out.println(result + "mm");

        // *** Here lies the Problem. If the Solution is right, it should print out: "Right".
        int nutzereingabe2 = scn.nextInt();
        String nutzerEingabe = scn.next();

        String nutzerEingabe3 = nutzereingabe2 + nutzerEingabe;
        if ( nutzerEingabe3 == (result+"mm")){
            System.out.println(result + "mm");
        }
   }

    // Measurment Generator
    private static String checkInput(int i) {
        String result = "";
        switch (i) {
        case 1:
            result = "mm";
            break;
        case 2:
            result = "cm";
            break;
        case 3:
            result = "m";
            break;
        case 0:
            result = "dm";
            break;
        default:
            result = "Error";
        }

        return result;
    }
}
Brian
  • 3,850
  • 3
  • 21
  • 37
  • @Steffan: Please [edit] your post with more details: Which "if" statement isn't behaving as you expect? What input do you run this program with, what output do you expect, and what output do you actually get? – Brian Aug 27 '18 at 20:13
  • 2
    At `nutzerEingabe3 == (result+"mm")` you are comparing strings by object identity. It should be `nutzer.equals(result+"mm")`. – LuCio Aug 27 '18 at 20:14
  • 1
    Please read [mcve] and enhance your question accordingly. "something doesn't work" isn't a working problem description. – GhostCat Aug 27 '18 at 20:14
  • Other than the fact that you are using `==` instead of `.equals()` which is a major issue in your **if** statement for comparing strings, where is the string variable **result** declared and receiving a string to hold? Is it in scope within the **main()** method? – DevilsHnd - 退職した Aug 27 '18 at 21:48
  • LuCio and DevilsHnd! Thanks for your help. That was exacly it!!! My Programming knowledge isnt that deep jet. Thank you soo much. – Seffan Aug 28 '18 at 17:09

0 Answers0