返回if语句中的值 [英] Returning the values inside an if statement

查看:163
本文介绍了返回if语句中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}




}





非常简单,真的。我想知道如何获得要打印的总值。我对此是一个菜鸟 - 谷歌搜索肯定没有帮助 - 所以一些解释的帮助将被广泛赞赏。



我有什么试过:



我尝试在每个if语句的末尾使用return语句,在int total声明之后,但遇到了编译错误。



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.

推荐答案

你必须在
int roll1 = (int) (Math.random() * 1000 % 6 + 1);

因为你将在dieRoll函数中使用它。

since you will use it within dieRoll function.

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

在main方法中声明一个变量来调用diceRoll函数。然后将返回值存储到变量。

Declare a variable inside your main method to call the diceRoll function. Then store the return value to the variable.


没有编译错误是一个好的开始,但还不够,显然你不明白这个代码是做什么的。使用调试器是一种非常好的学习方式,因为你看到你的代码正在运行。

当然,我可以给你答案,这将是魔术,你什么都不会学习,所以试一试调试器,你会自己理解和学习很多东西。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
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.


这篇关于返回if语句中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆