gpt4 book ai didi

java - 如何解决无输出问题-使用while-?

转载 作者:行者123 更新时间:2023-12-02 06:23:55 25 4
gpt4 key购买 nike

我尝试读取“Mi4”的每个字符如果是字母,则将其放入变量“大写”中如果是数字,则将其放入变量“num”中没有错误,也没有输出“

public static void main(String[] args) {
String capital = "";
int num = 1;
String sentence = "Mi4";
int senLength = sentence.length();
int i = 0;
while (i < senLength) {
String senStr = sentence.substring(i, i + 1);
char senChar = senStr.charAt(i);
if (senChar >= 'A' && senChar <= 'Z') {
capital = senStr;
} else if (senChar >= 'a' && senChar <= 'z') {
capital = capital + senStr;
} else if (senChar >= '2' && senChar <= '9') {
num = Integer.parseInt(senStr);
}
i++;
sentence = sentence.substring(i);
}
System.out.println(capital);
System.out.println(num);
}

最佳答案

我已经尝试过你的代码,下面提到了很多错误以及解决方案

String capital="";
int num=1;
String sentence= "Mi4";
int senLength= sentence.length();
int i=0;
while (i < senLength)//; in the while loop
{
//String senStr=sentence.substring(i,i+1); substring should be of 1st letter so (i,i+1) doesnot find letter in next iteration
char senChar= senStr.charAt(i);
if (senChar >= 'A' && senChar <='Z')
{capital= senStr;}
else if (senChar >= 'a' && senChar <='z')
{capital= capital+senStr;}
else if (senChar >='2' && senChar<='9')
{num= Integer.parseInt(senStr);}
i++;
//sentence=sentence.substring(i);same here in next iteration the string doesnot contains same lenght so it doesnot find char at i
}
System.out.println(capital);
System.out.println(num);

关于java - 如何解决无输出问题-使用while-?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55808039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com