Java mesurements

108 views Asked by At

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

There are 0 answers