gpt4 book ai didi

java - 使用单独的包进行程序输出

转载 作者:行者123 更新时间:2023-11-30 04:50:30 24 4
gpt4 key购买 nike

我正在学习包。我有两个位于不同包中的类,我的输出似乎有问题。

这是主要的类:我的两个问题是底部两部分“必需的字符串测试”和“字符串选择测试”

import myUtils.util.Console;

public class ConsoleTestApp
{
public static void main(String args[])
{
// create the Console object
Console c = new Console();

// display a welcome message
c.println("Welcome to the Console Tester application");
c.println();

// int
c.println("Int Test");
int i = c.getIntWithinRange("Enter an integer between -100 and 100: ", -101, 101);
c.println();

// double
c.println("Double Test");
double d = c.getDoubleWithinRange("Enter any number between -100 and 100: ", -101, 101);
c.println();

// required string
c.println("Required String Test");
String s1 = c.getRequiredString("Enter your email address: ");
c.println();

// string choice
c.println("String Choice Test");
String s2 = c.getChoiceString("Select one (x/y): ", "x", "y");
c.println();
}
}

这是他们正在访问的类的代码。 (这是在 main 顶部导入的 Console 类)

package myUtils.util;
import java.util.Scanner;

public class Console
{
Scanner sc = new Scanner(System.in);

/******************************************************/
public void println()
{

}

/******************************************************/
public void print(String s)
{
System.out.println(s);
}

/******************************************************/
public void println(String s)
{
System.out.println(s);
}

/*****************************************************/
public int getIntWithinRange (String prompt, int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
if (i < min)
{ System.out.println
("Error! Please enter an integer greater than -100");
}
else if (i > max)
{
System.out.println ("Error! Please enter an integer less than 100");
}
else
isValid = true;
}
else
{
System.out.println ("Error! Invalid number value");
sc.nextLine();
}
}
return i;
}

/*****************************************************/
public double getDoubleWithinRange (String prompt, double min, double max)
{
int d = 0 ;
boolean isValid = false;
while (isValid == false)
{
System.out.println(prompt);
if (sc.hasNextInt())
{
d = sc.nextInt();
if (d < min)
{
System.out.println ("Error! Please enter a number greater than -100");
}
else if (d > max)
{
System.out.println ("Error! Please enter a number less than 100");
}
else
isValid = true;
}
else
{
System.out.println("Error! Invalid number value");
sc.nextLine();
}
}
return d;
}

/*****************************************************/
public String getRequiredString(String prompt)
{
String R = "";
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
R = sc.nextLine();
if (R.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
}
else
{
isValid = true;
}
}
return R;
}

public String getChoiceString (String prompt, String s1, String s2)
{
String s = "";
boolean isvalid = false;
while (isvalid == false);
{
s = this.getChoiceString(prompt, s1, s2);
System.out.println(s);
s = sc.nextLine();
if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
{
System.out.println("Error! Entry must be '"+
s1 +"', '"+ s2 +"', or '."+ "Try again.");
}
else
{
isvalid = true;
}
}
return s;
}

public int getInt(String prompt)
{
return 0;
}
}

我在 getRequiredString 方面遇到的问题是我有一行代码,上面写着

    R = sc.nextLine();

这导致我的输出打印错误消息,因为它认为用户没有输入任何内容。当我把它改成简单的

    R = sc.next();

该代码不允许用户输入任何内容。我不确定如何解决这个问题。

我的 getChoiceString 遇到的问题是程序没有打印任何内容。看来该代码部分不会打印我的提示或任何其他元素。

我是新来的,这是家庭作业,因此任何提示、线索或帮助我完成这个过程的帮助都会受到赞赏。

最佳答案

明白了!您在 while 语句后添加了一个分号,这会导致程序停止。

while (isvalid == false);  <---
{
....
}

我已将其删除,并注意到您的代码进入无限循环,因为 getChoiceString 方法不必要地调用自身。因此我也对其进行了编辑(注释掉了您的递归调用),它现在似乎可以工作了!请尝试以下操作:

public String getChoiceString(String prompt, String s1, String s2)
{

String s = "";
boolean isvalid = false;
while (true)
{
if(isvalid == true)
{
break;
}
//s = this.getChoiceString(prompt, s1, s2);
//System.out.println(s);
System.out.println("Enter your String: ");
s = sc.nextLine();
if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2))
{
System.out.println("Error! Entry must be '"+
s1 +"', '"+ s2 +"', or '."+ "Try again.");


}
else
{
isvalid = true;
}
}
return s;

}

编辑:同样在 getRequiredString 方法中,使用 R = sc.next(); 而不是 R = sc.nextLine(); 为了让你的程序正常运行。检查以下内容:

System.out.print(prompt);
R = sc.next();
System.out.println("Email address: " + R.toString());
if (R.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
}
else
{
isValid = true;
}

关于java - 使用单独的包进行程序输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956615/

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