2012-09-01 68 views
2

我正在开始Java编程,并且我写了一个程序来测试每个星球有多少个卫星。这是一个简短的版本,只有四个行星。如何避免在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”这么多次?随着更多的陈述,这变得非常单调乏味。请记住,我是初学者,这个代码是关于我目前能力的限制。

+4

你知道阵列吗? – Ryan

+0

不是用Java编写的,但是我在python中触及它们时没有太深入。 – WillumMaguire

+0

为什么你的'for'循环只能持续一次迭代?似乎你根本不需要它...... – Baz

回答

4

你可以使用数组,像这样:

String[] planets = { "Mercury", "Venus", "Earth", "Mars" }; 
int moons[] = { 0, 0, 1, 2 }; 

,并呼吁:

if (rI >= 0 && rI < planets.length) { 
    question(planets[rI], moons[rI]); 
} 
+0

这对我来说非常合理,因为switch语句仍然涉及一长串事件。感谢伟大的,易于理解的答案! – WillumMaguire

+0

不客气。我已经添加了一个'警卫'声明来检查你是否在界限内。 :) – Reimeus

0

查看Java中的switch声明。这是旨在帮助克服扭曲IF-ELSEIF-ELSEIF-ELSEIF-else块:

switch(rI){ 
    case 2: question(two, 1); 
      break; 
    case 3: question(three, 2); 
      break; 
    default: doSomethingClever(...); 
} 

有,你可以使用其他的清理,但得到的是开关组到位解决您最初的评论。祝你好运。

0

您可以尝试使用switch而不是长的if-else阶梯。

有关switch更多信息,请阅读java documentation

你的情况可能是这样的:

switch (rI) { 
    case 0: 
     question(zero, 0); 
     break; 
    case 1: 
     question(one, 0); 
     break; 
    case 2: 
     question(two, 1); 
     break; 
    case 3: 
     question(three, 2); 
     break; 
    default: 
     break; 
} 
0

您可以使用switch关键字:

switch (rI) { 
    case 0: 
     question(zero, 0); 
     break; 
    case 1: 
     question(one, 0); 
     break; 
    case 2: 
     question(two, 1); 
     break; 
    case 3: 
     question(three, 2); 
     break; 
    default: 
     // do something 
} 

代码default下当没有找到匹配时执行。

您还可以使用arrays

String[] planets = new String[]{"Mercury", "Venus", "Earth", "Mars"}; 
int[] nums = new int[]{0, 0, 1, 2}; 
... 
// in the loop: 
question(planets[rI], nums[rI]); 

当然,还有其他的方法来解决这个问题,但我认为人谁是刚刚学习这些应该做的伎俩。然而,有一件事你可能想研究一下(一旦你深入了解Java),就是Map的概念,就像Python中的字典。你可以保存一张地图,将一个行星(字符串)映射到它拥有的卫星数目(int)。

1

一种更好的方式来写这个代码。它更具可读性,并且可以很容易地进行更新。

import java.util.Scanner; 
import java.util.Random; 

public class one { 
    public static void main(String args[]){ 
     //SET VARIABLES 
     String planets []=new String[4]; 
     planets[0]="Mercury"; 
     planets[1]="Venus"; 
     planets[2]="Earth"; 
     planets[3]="Mars"; 
     int moons []=new int[4]; 
     moons[0]=0; 
     moons[1]=0; 
     moons[2]=1; 
     moons[3]=2; 
     //SET VARIABLES 
     while(true){ 
      System.out.println("Moons"); 
      Random r = new Random(); 
      int rI = r.nextInt(4); 
      question(planets[rI],moons[rI]); 
     } 
    } 

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

只要你的逻辑不发展,你可以用switch或if-else去。否则,您需要启动面向对象编程。 为从地球上继承的每个行星创建一个类“行星”和另一个类,并将行星特定信息添加到每个行星。那么当你计划为行星添加更多的问题时,你对未来有好处。

0

我将开始与一个POJO(普通Java对象)忘记了矢量的事情,把行星和它们的卫星一起,把这些物品放入数组:

class Planet { 

    int moons; 
    String name; 

    Planet(String name, int moons) { 
     this.name = name; 
     this.moons = moons; 
    } 

    public String getName() { 
     return this.name; 
    } 

    public getMoons() { 
     return this.moons; 
    } 

    public void question(){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many moons does " + this.name + " have? "); 
     int ans = input.nextInt(); 
     if (ans == this.moons){ 
      System.out.println("Correct!"); 
     } 
     else { 
      System.out.println("Incorrect!"); 
     } 
    } 
} 

public class one { 
    public static void main(String args[]){ 
     //SET VARIABLES 
     List<Planet> planets = new ArrayList<Planets>(); 
     Planet earth = new Planet("Earth", 1); 
     // put other planets 
     //SET VARIABLES 
     for (int x=1; x<=1000; x++) { 
      System.out.println("Moons"); 
      Random r = new Random(); 
      int rI = r.nextInt(planets.size()); 
      Planet p = planets.get(rI); 
      p.question(); 
     } 
    } 
} 
0

我宁愿在阵列枚举这里。看到这个enums tutorial(特别是行星的例子;))。为月球计数添加另一个字段(就像质量和半径)。