gpt4 book ai didi

java - 我该如何正确表达呢?使用 && 和 ||首次

转载 作者:行者123 更新时间:2023-11-30 06:47:08 26 4
gpt4 key购买 nike

我从来没有用过&&或||在我之前的课上,我完全不知道如何正确使用它们。在我看来,我在想像下面这样的东西会起作用,但它没有。

public class ScoreDice {
// TODO - write your code below this comment.
// You will need to write one method: scoreWithNumbers.
// scoreWithNumbers returns a score based on the values
// of its inputs, as such:
//
// - If both inputs are 1 or if both inputs are 6, then
// the returned score is 10
// - If both inputs are the same (but not 1 or 6), then
// the returned score is 8
// - If the inputs are different, the score is whatever the
// smaller value is (e.g., if given 3 and 4, it returns 3).
//
public static String scoreWithNumbers(int num1, int num2) {
if ((num1==6 && num2==6) || (num2==1 && num1==1)) {
return "10";
} else if ((1 < num1 || num1 < 6) && num2==num1) {
return "8";
}
}

public static int scoreWithSeed(long seed) {
Random random = new Random(seed);
int num1 = rollD6(random);
int num2 = rollD6(random);
return scoreWithNumbers(num1, num2);
}

public static int rollD6(Random random) {
// random.nextInt(6) returns a number between
// 0 and 5, inclusive. We want to simulate rolling
// a D6 (6-sided die) so we add one to this result.
return random.nextInt(6) + 1;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter seed: ");
long seed = input.nextLong();
System.out.println("Score: " + scoreWithSeed(seed));
}
}

最佳答案

尝试使用下面的代码,注释解释 if 语句的每一步发生的事情:

基本上我改变的是我确保你返回 int's 因为 TODO 没有说 scoreWithNumbers 应该返回字符串,同样我添加了获取两个不同数字的代码并将类型从 longint 因为 int 更适合你程序的 scoreWithNumbers() 部分的相关测试。

import java.util.Scanner;

public class ScoreDice {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first input number: ");
int firstInputInt = scanner.nextInt();
System.out.print("Enter second input number: ");
int secondInputInt = scanner.nextInt();
System.out.println("Returned score from the method scoreWithNumbers: " + scoreWithNumbers(firstInputInt, secondInputInt));
}

public static int scoreWithNumbers(int num1, int num2) {
// If both inputs are 1 or if both inputs are 6, then the returned score is 10
if ((num1==1 && num2==1) || (num1==6 && num2==6)) {
return 10;
// If both inputs are the same (but not 1 or 6 [we checked that above]), then the returned score is 8
} else if (num1 == num2) {
return 8;
// If the inputs are different (i.e if (num1 != num2)), the score is whatever the smaller value is (e.g., if given 3 and 4, it returns 3).
} else {
return Math.min(num1, num2);
}
}
}

示例用法(案例 1:两个输入均为 1 或者两个输入均为 6)

Enter first input number:  1
Enter second input number: 1
Returned score from the method scoreWithNumbers: 10

Enter first input number: 6
Enter second input number: 6
Returned score from the method scoreWithNumbers: 10

示例用法(案例 2:两个输入相同但不是 1 或 6)

Enter first input number:  4
Enter second input number: 4
Returned score from the method scoreWithNumbers: 8

示例用法(案例 3:两个输入不同)

Enter first input number:  3
Enter second input number: 5
Returned score from the method scoreWithNumbers: 3

N.B. 最后的 else 可以删除,您可以只使用 return Math.min(num1, num2) 因为那部分只有在 ifelse if block 都没有事先评估的情况下,才会到达代码。

关于java - 我该如何正确表达呢?使用 && 和 ||首次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46462074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com