Using charAt method, won't add them as an Int, and wont print as string. Will explain better

后端 未结 4 1257
旧时难觅i
旧时难觅i 2021-01-27 17:58

Alright, so here is my code:

import java.util.Scanner;

public class CarRental {

    public static String model;
    public static int letternum;
    public st         


        
4条回答
  •  臣服心动
    2021-01-27 18:05

    public static String model;
    public static int letternum;
    public static String plate;
    public static String letter;
    public static int total;              
    public static String alphabet = "abcdefghijklmnopqrstuvwxyz";
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        //System.out.println("Car Model:");
        //model = input.nextLine();
        System.out.println("License Plate: ");
        plate = input.nextLine();
    
        char one = plate.charAt(0);
        char two = plate.charAt(1);
        char three = plate.charAt(2);
        total = Integer.parseInt(one) + Integer.parseInt(two) + Integer.parseInt(three);
        letternum = total % 24;
    
        char letter = alphabet.charAt(letternum);
    
        System.out.println("" + letter + total);
    
    }
    

    you forgot to cast it to integer

提交回复
热议问题