gpt4 book ai didi

java - 如果选择相同则扫描仪

转载 作者:行者123 更新时间:2023-12-01 17:35:44 25 4
gpt4 key购买 nike

如果选择相同,我需要扫描仪才能使用,但这不起作用,有什么建议来修复它吗?

我需要它,所以当输入等于 32、64、128 或 356 时,它将输出相关行,但是当我运行代码时,我才得到这个,错误:(18, 18)java找不到符号符号:变量等于location: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.");
}
}
}

最佳答案

您正在扫描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("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.");
}
}

关于java - 如果选择相同则扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045584/

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