我想在枚举中添加一个toString方法CustomerType.我的班级返回System.out.println()折扣百分比消息,如果取决于我cutomerType现在.20因为它是customerType大学.我是枚举的新手,我希望能够toString在我的枚举中添加一个方法,根据客户类型打印"College customer".我在完成这个方面遇到了一些麻烦?我究竟做错了什么?
听到我的班级:
import java.text.NumberFormat;
public class CustomerTypeApp
{
public static void main(String[] args)
{
// display a welcome message
System.out.println("Welcome to the Customer Type Test application\n");
// get and display the discount percent for a customer type
double Customer = getDiscountPercent(CustomerType.College);
NumberFormat percent = NumberFormat.getPercentInstance();
String display = "Discount Percent: " + percent.format(Customer);
System.out.println(display);
}
// a method that accepts a CustomerType enumeration
public static double getDiscountPercent (CustomerType ct) …Run Code Online (Sandbox Code Playgroud) 我有一个列表视图,其中填充了我希望能够单击某个项目并将其转到我播放音频的其他活动的国家/地区.我的清单查看:
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources()
.getStringArray(R.array.countries)));
}
}
Run Code Online (Sandbox Code Playgroud)
我如何添加一个onClick,就像你为一个按钮做一个?
我想缩短我的文本字段,因此它不会延伸到我的jframe的末尾,所以这就是它现在的样子:

如何控制文本字段的宽度,以便它不像我尝试setPreferedSize()和setSize()那样streatch他们没有工作?
@Override
public void run() {
JFrame frame = new JFrame("Test Calculator");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(500, 500);
JPanel panel = new JPanel(new GridBagLayout());
frame.getContentPane().add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel testLabel = new JLabel("Enter Score For Test 1: ");
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(40, 15, 15, 0);
panel.add(testLabel , c);
JTextField txtField1 = new JTextField("TextField");
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = .5; …Run Code Online (Sandbox Code Playgroud) 我想编写一个不接受任何参数的构造函数,所以如果我没有实例变量,我将如何做到这一点,用于创建一个构造函数,我有实例变量,我知道java创建一个默认构造函数,如果我没有但我被告知这是一个糟糕的编程习惯???(新的课程)
public class Validator {
public Validator() {
}
public String getString(Scanner sc, String prompt) {
System.out.print(prompt);
String s = sc.next(); // read user entry
sc.nextLine(); // discard any other data entered on the line
return s;
}
public int getInt(Scanner sc, String prompt) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // …Run Code Online (Sandbox Code Playgroud) 该应用程序是一个数字猜谜游戏,我的问题是当用户获得正确的随机数时,它询问用户是否要继续,如果他键入"y"它会创建一个新的随机数并要求用户猜测它.然后它应该询问用户"输入一个数字".相反,我得到了
"太高"(或"太低")
"输入数字"
输出:
输入数字:
2
正确你得到它!数字是2你在2次尝试得到它.
你想再次发挥(Y/N):
Ÿ
太低了!再试一次.
输入数字:
我如何得到它而不是打印"太高"或"太低"的问题,这是在他输入一个数字后确定的?
PS.我尝试了很多方法,但卡住了:(
public static void main(String[] args) {
System.out
.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
System.out
.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println();
System.out
.println("Am thinking of a number between 0 and 10. Try to guess it.");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
double rightNum = Math.random() * 10;
int randomNum = (int) rightNum; // convert the random number to int …Run Code Online (Sandbox Code Playgroud) 我如何使用compareToIgnoreCase字符串类的方法来查看字符串是否小于或更大,然后是另一个字符串?看到我不能用" >"和" <".