- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开始 Java 编程,我编写了一个程序来测试每个行星有多少颗卫星。这是一个只有四个行星的简化版本。
import java.util.Scanner;
import java.util.Random;
public class one {
public static void main(String args[]){
//SET VARIABLES
String zero="Mercury", one="Venus", two="Earth", three="Mars";
//SET VARIABLES
for (int x=1; x<=1000; x++){
System.out.println("Moons");
Random r = new Random();
for (int y=1; y<=1; y++){
int rI = r.nextInt(4);
if (rI == 0){
question(zero,0);
}
else if (rI == 1){
question(one, 0);
}
else if (rI == 2){
question(two, 1);
}
else if (rI == 3){
question(three, 2);
}
}
}
}
public static void question(String n, int num){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + n + " have? ");
int ans = input.nextInt();
if (ans == num){
System.out.println("Correct!");
}
else if (ans != num){
System.out.println("Incorrect!");
question(n, num);
}
}
}
我该怎么做才能不必多次写“else if”?随着更多的陈述,这变得非常乏味。请记住,我是初学者,这段代码几乎是我目前能力的极限。
最佳答案
你可以像这样使用数组:
String[] planets = { "Mercury", "Venus", "Earth", "Mars" };
int moons[] = { 0, 0, 1, 2 };
并调用:
if (rI >= 0 && rI < planets.length) {
question(planets[rI], moons[rI]);
}
关于java - 如何在 Java 中避免 else if 马拉松?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231011/
我正在尝试在我的基础架构上启动 rstudio。 如果我做 : sudo docker run -d -p 8787:8787 192.168.0.38:5000/rocker/rstudio 然后我
Marathon 和 Aurora 都是基于 Mesos 构建的,据说是为运行长时间运行的服务而设计的。我的问题是: 它们有什么区别?我一直在努力寻找关于它们的关键差异的任何好的解释 这些框架是否可以
我们将一个Spring Boot应用程序部署在Docker容器中并使用中层(marathon + mesos)进行管理。 Spring Boot应用程序旨在通过马拉松部署,一旦完成,它将退出,代码=
我正在尝试使用 telegram API 和 Telethon 删除消息。 这是我的代码: from telethon import InteractiveTelegramClient from te
我在我的两台机器上设置了 Marathon 和 Mesos。 我可以从 marathon Web 控制台成功调度命令,但是当我尝试调度涉及 docker 图像的作业时,我立即发现作业失败。另外,我没有
我有带有Marathon框架的3节点Mesos集群。在从属服务器上,我有Docker,我想在一个节点上部署几个Wildfly实例。 如何通过Marathon在一个从属Mesos节点上部署Wildfly
我正在开始 Java 编程,我编写了一个程序来测试每个行星有多少颗卫星。这是一个只有四个行星的简化版本。 import java.util.Scanner; import java.util.Rand
如何使用 Telethon 创建公共(public)/私有(private) channel ?我在官方文档中没有找到这个信息。 最佳答案 您可以使用此过程来创建私有(private) channel
我是一名优秀的程序员,十分优秀!