Java 为什么我的代码中没有While循环的输出,尽管其他一切都在工作?

Java 为什么我的代码中没有While循环的输出,尽管其他一切都在工作?,java,while-loop,Java,While Loop,我在BlueJ IDE上用java编写了这个程序。它的意思是取一个十进制的数字,并将其转换为用户选择的基数,直到基数9为止。它是通过取两个数字之间的模并将其插入字符串来实现的。代码一直工作到输入阶段,之后就没有输出了。我确信我的数学是正确的,但语法可能有问题 我的代码如下: import java.util.*; public class Octal { public static void main(String[] args) { Scanner in =

我在BlueJ IDE上用java编写了这个程序。它的意思是取一个十进制的数字,并将其转换为用户选择的基数,直到基数9为止。它是通过取两个数字之间的模并将其插入字符串来实现的。代码一直工作到输入阶段,之后就没有输出了。我确信我的数学是正确的,但语法可能有问题

我的代码如下:

import java.util.*;
public class Octal
{

    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);

        int danum = 0;
        int base = 0;
        System.out.println("Please enter the base you want the number in (till decimal). Enter as a whole number");
        base=in.nextInt(); //This is the base the user wants the number converted in//
        System.out.println("Enter the number you want converted (enter in decimal)");
        danum=in.nextInt(); //This is the number the user wants converted//

        while ( danum/base >= base-1 && base < danum) {
           int rem = danum/base; //The number by the base//
           int modu = danum % base;//the modulus//

            String summat = Integer.toString(modu);//this is to convert the integer to the string//
            String strConverted = new String();//Making a new string??//
            StringBuffer buff = new StringBuffer(strConverted);//StringBuffer command//
            buff.insert(0, summat); //inserting the modulus into the first position (0 index)//

            danum = rem;

            if ( rem <= base-1 || base>danum) {//does the || work guys?//
                System.out.println(rem + strConverted);
            }
           else {
               System.out.println(strConverted);
            }

}
}
}
import java.util.*;
公共级八进制
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
int-danum=0;
int base=0;
System.out.println(“请输入输入数字的基数(直到小数点)。以整数形式输入”);
base=in.nextInt();//这是用户希望转换数字的基数//
System.out.println(“输入要转换的数字(输入十进制)”;
danum=in.nextInt();//这是用户希望转换的数字//
而(达南/碱>=碱-1和碱<达南){
int rem=danum/base;//以基数表示的数字//
int modu=danum%base;//模数//
String summat=Integer.toString(modu);//用于将整数转换为字符串//
String strConverted=new String();//创建新字符串//
StringBuffer buff=新的StringBuffer(strConverted);//StringBuffer命令//
buff.insert(0,summat);//将模数插入第一个位置(0索引)//
丹麦=雷姆;
如果(雷姆·达纳姆){//| | |的人工作吗//
系统输出打印项次(rem+strConverted);
}
否则{
系统输出打印项次(strConverted);
}
}
}
}
我对Java非常陌生,所以我不完全了解它的语法。我已经尽了最大的努力去研究,这样我就不会浪费你的时间了。请给我一些关于如何改进我的代码和我作为程序员的技能的建议。谢谢。

编辑(前面的答案显然是反应太快了…)

实际上,
strConverted
仍然是一个空字符串,也许您宁愿显示
buff.toString()

但我真的不明白为什么要把所有这些都变成只显示
modu
的值。您可以直接
System.out.println(modu)

我假设您希望“保存”您的值,并一次性显示您的整数,而不是逐行显示每个数字

因此,当循环时,您需要将您的号码存储在
之外,否则每次调用循环时,您的字符串都将是init。(并在外部打印)

因此,在循环外部初始化
StringBuffer
。您不需要将
int
转换为
String
,因为
StringBuffer
接受
int

(您甚至可以使用
StringBuilder
而不是
StringBuffer
。它的工作原理与
StringBuffer
的工作原理相同 )

循环中的
如果
是特定情况(数字低于基数),则在循环之前禁止,因为这是循环的相反条件。(顺便说一句:
rem-danum
实际上只是一个测试,因为
rem==danum
在这个地方)

因此:

StringBuffer buff=新的StringBuffer();
if(碱>丹){
浅黄色附加(丹麦);
}否则{
而(达南/碱>=碱-1和碱<达南){
int rem=丹/碱;
int modu=基准的丹麦百分比;
buff.插入(0,modu);
丹麦=雷姆;
}
如果(danum>0){
浅黄色插入物(0,达南);
}
}
System.out.println(buff.toString());
我还强烈建议在运行代码之前测试您的输入。(没有零表示基数,没有字母等。)

2件事

  • 在获得用户输入后,执行更多的错误检查。它避免了在路径上出现奇怪的“错误”

  • 循环中从int到String的转换是错误的。整件事是怎么回事修改缓冲区不会影响转换后的strConverted(因此您看到的总是空的)

  • 试着摆脱它。:)


  • 错误与逻辑有关
  • 错误与java相关

  • 您的代码存在以下问题:

  • 首先,您已经在
    while
    循环中声明并初始化了
    strConverted
    变量(在其中存储结果)。因此,每当循环重复时,它都会创建一个新字符串
    strConverted
    ,其值为
    。因此,你的答案永远不会是正确的
  • 其次,
    StringBuffer buff
    从不更改字符串
    strConverted
    。您必须通过实际调用来更改字符串
  • 您可以在
    中打印结果,而
    循环会在每次重复后打印一步一步的结果。必须在循环内更改strConverted的值,但最终结果必须在循环外打印

  • 使用调试器找出您自己。。。。如果您不能使用调试器,只需添加一些sysout语句来验证您的数学(例如while循环条件)…如果代码生成并运行,语法不会有问题。你用什么样的例子试过?在while循环之前打印
    base
    danum
    ,看看它们是否得到正确的值。我打赌程序会异常退出,而不是正常退出。我认为你在第一次测试while条件时是除以零。您能否编辑您的问题并添加运行程序后得到的异常/输出类型?它们由用户输入,因此让我们假设有效值-缺少错误检查-不适用于1和1。either@Daij-Djan现在应该好多了:)注:重写整个过程,从纸上开始。算法运行后,将java添加到混合中
    String summat = Integer.toString(modu);
    String strConverted = new String();
    StringBuffer buff = new StringBuffer(strConverted);
    buff.insert(0, summat);
    ...
    
    System.out.println(strConverted);
    
    StringBuffer buff = new StringBuffer();
    
    if(base > danum) {
        buff.append(danum);
    } else {
        while (danum / base >= base - 1 && base < danum) {
            int rem = danum / base;
            int modu = danum % base;
            buff.insert(0, modu);
            danum = rem;
        }
    
        if(danum > 0) {
            buff.insert(0, danum);
        }
    }
    
    System.out.println(buff.toString());