- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在练习 Java,遇到了一些奇怪的问题,或者我应该说问题。
当我编译下面的代码时,它显示 java line # :error:
missing return statement.
如果我删除 return "not valid"
处的评论,它编译。
现在这就是另一个问题出现的地方。当我通过init_config
时和final_config
我输入的字符串 A
和B
,它返回无效。但是当我通过"A"
时和"B"
到其他函数( other("A", "B")
返回/打印特定返回值 "C"
。
我不确定问题是否出在我的输入法上。我为 init_config 和 Final_config 输入的数据应该是字符串值,对吗?我不确定 Scanner 是否是一个好的字符串输入方法。但是,如果我打印两个输入,它工作正常,所以我不确定是否是数据丢失或字符串引用在传递时丢失。
我还尝试替换 init_config = in.next()
与 init_config = in.nextLine()
但这没有任何区别。
是否需要用return "not valid"
来编译代码在函数末尾或者我可以通过某种方法绕过它吗?如何使用 Scanner
传递字符串数据输入法没有任何损失?
这是我的代码:
import java.util.Scanner;
public class towerGen
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
String init_config, final_config;
System.out.print("Enter initial configuration: ");
init_config = in.next();
System.out.print("Enter final configuration: ");
final_config = in.next();
System.out.print(other(init_config, final_config));
}
public static String other(String src, String dest)
{
if (src=="A" && dest=="B")
return "C";
if (src=="B" && dest=="A")
return "C";
if (src=="B" && dest=="C")
return "A";
if (src=="C" && dest=="B")
return "A";
if (src=="A" && dest=="C")
return "B";
if (src=="C" && dest=="A")
return "B";
//return "not valid";
}
}
最佳答案
Is it necessary to compile the code with the return "not valid" at the end of the function?
是的,确实如此。因为编译器认识到,如果输入字符串不满足您列出的任何条件,则不会返回任何内容,这是非 void 方法中的错误。
此外,在您的 other
方法中,您应该使用 src.equals("A")
而不是 src == "A"
比较字符串时。
关于Java 不从扫描仪输入返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370356/
我在使用 Scanner 类中的 useDelimiter 时遇到一些问题。 Scanner sc = new Scanner(System.in).useDelimiter("-"); while(
我是 Java 新手。 我目前正在做一个业余项目;制作基于文本的游戏。我意识到使用 Switch 语句对于此类游戏非常有用。 这基本上就是它的工作原理。 我问用户,你想做什么? 吃 步行 等等 那么,
我正在尝试使用扫描仪从“p.addPoint(x,y);”形式的字符串中读取代码行 我想要的正则表达式格式是: *任何内容*.addPoint(*空格或无* 或 ,*空格或无* 到目前为止我所尝试的方
我正在使用 java Scanner 尝试从名为 Inventory.txt 的文本文件中提取产品信息。 此文件包含以下格式的产品数据: “Danelectro|Bass|D56BASS-AQUA|3
我是java初学者,我正在努力让这段代码正常工作。我正在尝试读取 CSV 文件,然后使用该数据计算平均值,然后返回平均值的最高最低值和平均值的摘要。 输入如下所示: Alicia Marks,89,9
当我进入/忽略文件 reg.txt 中的最后一个新行时,我需要一些有关退出的帮助。截至目前,当它到达最后一行时,我收到一个错误,其中不包含任何内容。 public String load() {
我的程序应该提示用户输入与参加了多少次考试相关的考试分数。然而,这工作得很好,当用户输入负的考试分数时,应该让他们再次重新进入所有三项考试。我的程序会保存任何非负面的考试,因此当您重新输入三项考试时,
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 9 年前。 下面给出的
我想读取用户输入,例如:11 12 13 14 15 16 Scanner sc = new Scanner(System.in); while(sc.hasNext()){
String[] invalidChars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; Scanner sc = ne
我有一个txt文件,每行包含两个单词,例如: USA 321 France 1009 ... Germany 902 如何在二维数组中逐字读取该文件?我有: List> temps = new Arr
我的 Java 作业有问题。我收到意外异常,特别是: java.util.NoSuchElementException: No line found 我正在使用 Scanner(System.in)
我有一个很大的困惑,当我们有 Sonar 服务器时 Sonar 扫描仪有什么用?当我使用 soarqube 服务器分析一个项目时,它进行了分析并且运行良好。我仍然很困惑为什么我们也需要扫描仪。 与ec
为什么我在递归方法中遇到无限循环,而没有机会输入任何符号来打破它? class Test { int key=0; void meth(){ System.out.println
我在运行此函数时遇到错误。它使用扫描仪在某个文件中查找单词。 这里是: public static boolean VerifyExistWord(File FileToSearch, String
各位程序员大家好。 我有一些代码,spring工具套件编辑器的响应也不同,也许你们中的一些聪明人知道为什么。 File inputFile = new File(System.getProperty(
是否可以从我的网络应用程序中使用 Flash 访问通用 twain 扫描仪,保存文件并将其上传到我的应用程序中? 我已经通过谷歌进行了一些搜索,但无法找到更多细节。是否有任何预制的解决方案,付费/免费
我试图在 float 组中引入一组 float 字: protected float[] a = new float [100]; public void setCoef(){ System.
我一直在做一项充当拼字游戏词典的编程作业。该程序接受用户的输入,并根据用户从菜单中请求的内容输出包含单词列表的文件。我遇到的问题与 Scanner.nextLine() 有关。 我不太清楚为什么,但由
我试图让程序询问百分比(等级),但我希望它在用户进行第一个输入并看到输出后再次询问。我在循环中遇到问题,因为未分配变量 myMark。 import java.util.Scanner; public
我是一名优秀的程序员,十分优秀!