如果选择相等,则进行扫描

时间:2020-04-05 16:14:19

标签: java

如果选择相等,我需要扫描仪配合使用,但这不起作用,有任何建议可以解决吗?

我需要它,所以当输入等于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.");
    }
    }
}

1 个答案:

答案 0 :(得分: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("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.");
        }
    }