Click here to Skip to main content
15,915,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package com.company;

public class MinValue {
static int dieRoll() {
    int roll1 = (int) (Math.random() * 1000 % 6 + 1);
    int roll2 = (int) (Math.random() * 1000 % 6 + 1);
    int roll3 = (int) (Math.random() * 1000 % 6 + 1);
    int roll4 = (int) (Math.random() * 1000 % 6 + 1);
    int[] numbers = {roll1, roll2, roll3, roll4};
    int minValue = numbers[0];
    for (int i = 1; i < numbers.length; i++) {
        if (numbers[i] < minValue) {
            minValue = numbers[i];
        }
        if (minValue == roll1) {
            int total = roll4 + roll2 + roll3;
            
        } else if (minValue == roll2) {
            int total = roll1 + roll4 + roll3;
            
        } else if (minValue == roll3) {
            int total = roll1 + roll2 + roll4;
            
        } else if (minValue == roll4) {
            int total = roll1 + roll2 + roll3;
            
        }
    }
    return total;
}


public static void main(String[] args){
    System.out.println(dieRoll());

}




}


Pretty straightforward, really. I want to know how to get the value for total to be printed. I'm a noob at this - and googling around definitely didn't help - so some help with some explanation would be widely appreciated.

What I have tried:

I tried using the return statement at the end of each and every if statement, following the declaration of int total, but ran into compilation errors.
Posted
Updated 25-Feb-18 20:44pm
Comments
Patrice T 26-Feb-18 2:36am    
Show code that give errors and error messages.

You have to declare total before
int roll1 = (int) (Math.random() * 1000 % 6 + 1);
since you will use it within dieRoll function.
Java
static int dieRoll() {
        int total = 0;
           ....
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] < minValue) {
                minValue = numbers[i];
            }
            if (minValue == roll1) {
                 total = roll4 + roll2 + roll3;

            } else if (minValue == roll2) {
                 total = roll1 + roll4 + roll3;

            } else if (minValue == roll3) {
                 total = roll1 + roll2 + roll4;

            } else if (minValue == roll4) {
                 total = roll1 + roll2 + roll3;

            }
        }
        return total; 
    }
Declare a variable inside your main method to call the diceRoll function. Then store the return value to the variable.
 
Share this answer
 
No compilation error is a good start but is not enough, and obviously you don't understand what is doing this code. Using the debugger is a very good way to learn, because you see your code performing.
Surely, I can just give you the answer, it will be magic and you will learn nothing, so give a try to debugger and you will understand and learn a lot by yourself.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900