英文:
Scanner if Choice Equals
问题
以下是翻译好的部分:
import java.util.Scanner;
import java.util.Random;
public class Main {
public static final String ALPHA_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz";
static Scanner scan = new Scanner(System.in);
private static String getNum() {
Random r = new Random();
int offset = r.nextInt(ALPHA_CHARS.length());
return ALPHA_CHARS.substring(offset, offset + 1);
}
public static void main(String[] args) {
System.out.println("选择字符串长度,必须是 32、64、128 或 256 之一。");
System.out.println("输入一个值。");
String choice;
choice = scan.nextLine();
if (choice.equals("32")) {
System.out.println("生成 32 个字符。");
System.out.println(getNum());
}
if (choice.equals("64")) {
System.out.println("生成 64 个字符。");
}
if (choice.equals("128")) {
System.out.println("生成 128 个字符。");
}
if (choice.equals("256")) {
System.out.println("生成 256 个字符。");
} else {
System.out.println("请选择一个有效的整数。");
}
}
}
注意:我已经将代码中的 "
替换为正常的双引号 "
,并修复了 equals
的用法,使其与字符串进行比较。如果您需要进一步的帮助,请随时提问。
英文:
I need the scanner to work with if choice equals, however this doesnt work, any suggestions to fix it?
I need it so when the input equals either 32, 64, 128, or 356 it will output the relevant lines, however i just get this when i run the code, Error:(18, 18)
java cannot find symbol
symbol: variable equals
location: variable choice of type java.lang.string
package com.company;
import java.util.Scanner;
import java.util.Random;
public class Main {
public static final String ALPHA_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz";
static Scanner scan = new Scanner(System.in);
private static String getNum() {
Random r = new Random();
int offset = r.nextInt(ALPHA_CHARS.length());
return ALPHA_CHARS.substring(offset, offset+1);
}
public static int Length = 0;
public static void main(String[] args) {
System.out.println("Choose the Strings length, It must be either 32, 64, 128 or 256 characters.");
System.out.println("Enter a value.");
String choice;
choice = scan.nextLine();
if(choice.equals == 32) {
System.out.println("Generating 32 Characters.");
System.out.println(getNum());
}
if(choice.equals == 64) {
System.out.println("Generating 64 Characters.");
}
if(choice.equals == 128) {
System.out.println("Generating 128 Characters.");
}
if(choice.equals == 256) {
System.out.println("Generating 256 Characters.");
}
else {
System.out.println("Choose a valid integer.");
}
}
}
答案1
得分: 0
你正在扫描一个“String”并将其与一个“int”进行比较。此外,在这种情况下最好使用“else if”:
public static final String ALPHA_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz";
static Scanner scan = new Scanner(System.in);
private static String getNum() {
Random r = new Random();
int offset = r.nextInt(ALPHA_CHARS.length());
return ALPHA_CHARS.substring(offset, offset+1);
}
public static int Length = 0;
public static void main(String[] args) {
System.out.println("选择字符串的长度,必须为32、64、128或256个字符之一。");
System.out.println("输入一个值。");
int choice;
choice = scan.nextInt();
if(choice == 32) {
System.out.println("生成32个字符。");
System.out.println(getNum());
}else if(choice == 64) {
System.out.println("生成64个字符。");
}else if(choice == 128) {
System.out.println("生成128个字符。");
}else if(choice == 256) {
System.out.println("生成256个字符。");
}else {
System.out.println("请选择一个有效的整数。");
}
}
英文:
You are scanning a String
and comparing it to an int
. Besides, it is better to use else if
in such cases:
public static final String ALPHA_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz";
static Scanner scan = new Scanner(System.in);
private static String getNum() {
Random r = new Random();
int offset = r.nextInt(ALPHA_CHARS.length());
return ALPHA_CHARS.substring(offset, offset+1);
}
public static int Length = 0;
public static void main(String[] args) {
System.out.println("Choose the Strings length, It must be either 32, 64, 128 or 256 characters.");
System.out.println("Enter a value.");
int choice;
choice = scan.nextInt();
if(choice == 32) {
System.out.println("Generating 32 Characters.");
System.out.println(getNum());
}else if(choice == 64) {
System.out.println("Generating 64 Characters.");
}else if(choice == 128) {
System.out.println("Generating 128 Characters.");
}else if(choice == 256) {
System.out.println("Generating 256 Characters.");
}else {
System.out.println("Choose a valid integer.");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论