试图做一个布尔循环

时间:2015-09-21 09:21:58

标签: java loops boolean

我是新手,我想尝试做一个循环我有点怎么做但是我被卡住了。我收到这些错误并不确定该如何处理它。

  

重复本地变量分钟
   重复的局部变量选择

     

类型不匹配:无法从double转换为String
  运营商 /   未定义参数类型字符串,双重
  运营商 /   未定义参数类型字符串,双重
  类型不匹配:   无法从double转换为String

     `import java.util.Scanner;

   public class LoopTest {
   public static void main(String[] arge) {
    Scanner scanner = new Scanner(System.in);
    double minutes = 0;
    double minutesperday;
    double minutesperyear;
    double years;
    double days;
    double choice;

    boolean keepPlaying = true;
    while (keepPlaying) {
        String minutes;
        String choice;

        System.out.print("number of minutes");
         minutes = scanner.nextDouble();

        minutesperday = 60 * 24;
        minutesperyear = minutesperday * 365;

        years = (minutes / minutesperyear);
        days = (minutes / minutesperday) % 365;

        System.out.print(minutes + " is about" + "\nYears:" + years  + "\nDays:" + days);

        System.out.print("\nWant to enter more minutes (y/n)? ");
        choice = scanner.nextDouble();

        if (choice.equals("y")) {
            keepPlaying = true;
        } else {
            keepPlaying = false;
        }

       }

}
}

2 个答案:

答案 0 :(得分:1)

非常简单:因为错误消息已经说明了:

  

重复本地变量

这基本上意味着同一范围内存在两个具有相同名称的变量。在您的情况下String minuteschoice以及String minutes相同。实际上minutes / minutesperyear是导致其他错误的那个,因为java认为String minutes表示变量double minutes,而不是 var max: Bool = true func startAnimation() { max = !max let duration: Double = 1 let fullCircle = 2 * M_PI let upOrDown = (max ? CGFloat(-1 / 16 * fullCircle) : CGFloat(1 / 16 * fullCircle)) let scale: (CGFloat, CGFloat) = (max ? (1.0, 1.0) : (1.3, 1.3)) UIView.animateWithDuration(duration, animations: { () -> Void in let rotationAnimation = CGAffineTransformMakeRotation(upOrDown) let scaleAnimation = CGAffineTransformMakeScale(scale) self.startButton.transform = CGAffineTransformConcat(rotationAnimation, scaleAnimation) }) { (finished) -> Void in self.startAnimation() }

答案 1 :(得分:1)

  • 删除String choicedouble choice,您可以直接扫描boolean true/false并将其存储在keepPlaying

    < / LI>
  • 删除String minutes,因为您已经有一个可以存储会议记录的double minutes变量

  • 关闭scanner

更正后的代码

    Scanner scanner = new Scanner(System.in);
    double minutes = 0;
    double minutesperday;
    double minutesperyear;
    double years;
    double days;

    boolean keepPlaying = true;
    while (keepPlaying)
    {
        System.out.print("number of minutes");
        minutes = scanner.nextDouble();

        minutesperday = 60 * 24;
        minutesperyear = minutesperday * 365;

        years = (minutes / minutesperyear);
        days = (minutes / minutesperday) % 365;

        System.out.print(minutes + " is about" + "\nYears:" + years + "\nDays:" + days);

        System.out.print("\nWant to enter more minutes (true/false)? ");
        keepPlaying = scanner.nextBoolean();
    }
    scanner.close();