while语句在此代码中不起作用? [英] The while statement does not work in this code?

查看:65
本文介绍了while语句在此代码中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码。我不明白为什么程序结束而不要求用户输入,这决定程序是否继续运行。

它必须等待输入,不是吗?为什么不等待输入就终止?






package com.company;
import java.util.Scanner;

public class PP1CourseWork2 {
    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 tot = roll1 + roll2 + roll3;
        return tot;
    }
    public static void main(String[] args){
        System.out.println("Enter Level: ");
        Scanner sc = new Scanner(System.in);
        int Level = sc.nextInt();
        String choice = null;
        int Str = dieRoll();
        int Dex =  dieRoll();
        int Con = dieRoll();
        int Int = dieRoll();
        int Wis = dieRoll();
        int Cha = dieRoll();

        do{
            if (Str == 10) {
                System.out.println("Str: " + "[" + Str + "]" + "[0]");


            } else if (Str > 10) {
                int bonus = 0;

                for (int i = 11; i < Str + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Str: " + "[" + Str + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Str; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Str: " + "[" + Str + "]" + "[-" + bonus + "]");
            }
            if (Dex == 10) {
                System.out.println("Dex: " + "[" + Dex + "]" + "[0]");


            } else if (Dex > 10) {
                int bonus = 0;

                for (int i = 11; i < Dex + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Dex: " + "[" + Dex + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Dex; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Dex: " + "[" + Dex + "]" + "[-" + bonus + "]");
            }
            if (Con == 10) {
                System.out.println("Con: " + "[" + Con + "]" + "[0]");


            } else if (Con > 10) {
                int bonus = 0;

                for (int i = 11; i < Con + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Con: " + "[" + Con + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Con; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Con: " + "[" + Con + "]" + "[-" + bonus + "]");
            }
            if (Int == 10) {
                System.out.println("Int: " + "[" + Int + "]" + "[0]");


            } else if (Int > 10) {
                int bonus = 0;

                for (int i = 11; i < Int + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Int: " + "[" + Int + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Int; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Int: " + "[" + Int + "]" + "[-" + bonus + "]");
            }
            if (Wis == 10) {
                System.out.println("Wis: " + "[" + Wis + "]" + "[0]");


            } else if (Wis > 10) {
                int bonus = 0;

                for (int i = 11; i < Wis + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Wis: " + "[" + Wis + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Wis; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Wis: " + "[" + Wis + "]" + "[-" + bonus + "]");
            }
            if (Cha == 10) {
                System.out.println("Cha: " + "[" + Cha + "]" + "[0]");


            } else if (Cha > 10) {
                int bonus = 0;

                for (int i = 11; i < Cha + 1; i++) {
                    if (i % 2 == 0) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Cha: " + "[" + Cha + "]" + "[+" + bonus + "]");
            } else {
                int bonus = 0;
                for (int i = Cha; i < 10; i++) {
                    if (!(i % 2 == 0)) {
                        bonus = bonus + 1;
                    }
                }
                System.out.println("Cha: " + "[" + Cha + "]" + "[-" + bonus + "]");
            }

            choice= sc.nextLine();
            System.out.println("Type r if you want to re-roll or any other Character if you want to Continue: ");


        } while(choice.equals("r"));

    }
}





我的尝试:



我曾尝试使用for循环,但显然我需要它至少运行一次所以没有。我真的不知道为什么会这样。



What I have tried:

Ive tried this using for loops but obviously i need it to run atleast once so no. I really have no idea why this happens.

推荐答案

引用:

choice = sc.nextLine();

System.out.println(如果要继续,请键入r,如果要继续,请键入任何其他字符:);

choice= sc.nextLine();
System.out.println("Type r if you want to re-roll or any other Character if you want to Continue: ");

to

System.out.println("Type r if you want to re-roll or any other Character if you want to Continue: ");
choice= sc.next();


您应该在main方法的开头放置一个断点,然后启动调试会话。然后逐行执行并找出你得到这个结果的原因。


作为旁注,你还应该重构你的主要方法并摆脱这种意大利面条代码:)



例如,你只需要一种方法来获得相应的能力加值。

You should put a breakpoint at the beginning of your main method, and start a debug session. Then execute line-by-line and find out why you get this result.

As a side note, you should also refactor your main method and get rid of this spaghetti code :)

For example, you only need a method to get corresponding bonus to an ability.
int getBonus(int abilityValue) {
   return (abilityValue / 2) - 5;
}

它可以让你写出

It would allow you to write

bonus = getBonus(Str);

而不是使用循环基本上需要几个基本的算术运算。



亲切。

instead of using a loop for what requires basically a couple of basic arithmetic operations.

Kindly.


嗨我不知道是否这个问题是相关的,我试图找到它被问到但我设法以这种方式修复它



使用do -while(你已经)。与你的问题是你从键盘输入的方式。你不能从我所知道的那样采取它。使用命令



Hi i do not know if this question is relevant anymore i tried to find when it was asked but i managed to fix it in this manner

use do -while(which you have already). The issue with yours is the way that you are taking the input from the keyboard.you can not take it that way from what i know.Use the command

n= sc.next().charAt(0);





这是使用.charAt()获取键盘输入n是我为re-rolling.0分配的值是第一个值(如java编号中的第一个字母是0等等)





this takes the keyboard input using .charAt() n is the value i have assigned for re-rolling.0 is the first value (as in java numbering first letter is 0 and so on)

while(n=='n');



使用此格式作为格式,我不希望在此处发布代码,因为如果您迫切需要,我也会使用它来发送给我发送电子邮件至

yasanthsera@gmail.com


use this as the while format, i dont wish to post the code here as i am also doing something with it if you are in dire need send me an email on
yasanthsera@gmail.com


这篇关于while语句在此代码中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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