簡體   English   中英

Java 中的類型已定義錯誤

[英]The Type is Already Defined Error in Java

我正在努力完成我的作業,我必須將所有答案匯總到一個“.java”文件中。 但是,當我嘗試將所有類添加到一個文件中時,對於 que2、3 和 4,出現“類型 que2 已定義”錯誤。為什么會出現此錯誤? 但是如果我在單獨的文件中運行它們,我可以毫無錯誤地運行它們。 我習慣了 python,我從來沒有經歷過這樣的事情。謝謝你,祝你有美好的一天。 順便說一句,我正在使用日食。

/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 1 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
package tes;
import java.util.Scanner;
import java.util.Base64;    
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;   
import javax.crypto.SecretKey; 
public class tes {
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              int a = 0;
              while (a != 6) {
                  System.out.println("                      ");
                  System.out.println("Select Operation:");
                  System.out.println("1:Addition");
                  System.out.println("2:Subtraction");
                  System.out.println("3:Multiplication");
                  System.out.println("4:Division");
                  System.out.println("5:Remainder");
                  System.out.println("6:Exit");
                  System.out.println("                      ");
                  System.out.print("Enter a choice: ");
                  int n = sc.nextInt();
                  a = n;
                  
                  if (n==6) {
                     break;
                  } 
                  else if(n==1) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"+"+n2+" is "+(n1+n2));
                  }
                  else if(n==2) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"-"+n2+" is "+(n1-n2));
                  }
                  else if(n==3) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"*"+n2+" is "+(n1*n2));
                  }
                  else if(n==4) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     if(n2==0) {
                         System.out.println("Please change the n2 value.");
                     }
                     else {
                         System.out.println("Result of "+n1+"/"+n2+" is "+(n1/n2));
                     }                
                     }
                  else if(n==5) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"%"+n2+" is "+(n1%n2));
                  }
              }
          }
}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 2 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que2 {

    public static void main(String[] args) {
        String s1 = "Welcome to Java";
        String s2 = s1;
        String s3 = new String("Welcome to Java"); 
        String s4 = "Welcome to Java";
        
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
        System.out.println(s1.equals(s2));
        System.out.println(s2.equals(s3));
        System.out.println(s1.compareTo(s2));
        System.out.println(s1==s4);
        System.out.println(s1.charAt(0));
        System.out.println(s1.indexOf("j"));
        System.out.println(s1.indexOf("to"));
        System.out.println(s1.lastIndexOf("a"));
        System.out.println(s1.lastIndexOf("o", 15));
        System.out.println(s1.length());
        System.out.println(s1.substring(5));
        System.out.println(s1.substring(5, 11));
        System.out.println(s1.startsWith("Wel"));
        System.out.println(s1.substring(s1.lastIndexOf(" ")+1));
    }

}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 3 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que3 {
    public static void main(String []args) { 
        int sum=0;
        // iterate from 0 to N 
        for (int num = 0; num < 51; num++) 
        {    
            // Short-circuit operator is used  
            if (num % 3 == 0 && num % 5 == 0){
                sum+=num;
                System.out.println(num+"");  
        }  
    }   System.out.println("Sum is: ");
        System.out.println(sum); 
}
    
}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 4 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que4 {  
    static Cipher cipher;  

    public static void main(String[] args) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128); // block size is 128bits
        SecretKey secretKey = keyGenerator.generateKey();

        cipher = Cipher.getInstance("AES");

        String plainText = "AES Symmetric Encryption Decryption";
        System.out.println("Plain Text Before Encryption: " + plainText);

        String encryptedText = encrypt(plainText, secretKey);
        System.out.println("Encrypted Text After Encryption: " + encryptedText);

        String decryptedText = decrypt(encryptedText, secretKey);
        System.out.println("Decrypted Text After Decryption: " + decryptedText);
    }

    public static String encrypt(String plaTex, SecretKey sKey)
            throws Exception {
        byte[] Tb = plaTex.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, sKey);
        byte[] encryptedByte = cipher.doFinal(Tb);
        Base64.Encoder encoder = Base64.getEncoder();
        String encTex = encoder.encodeToString(encryptedByte);
        return encTex;
    }

    public static String decrypt(String encryptedText, SecretKey secretKey)
            throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] encryptedTextByte = decoder.decode(encryptedText);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
        String decTex = new String(decryptedByte);
        return decTex;
    }
}

如果您將que2作為tes類文件中的內部類和單獨的que2類文件,則可能會出現此錯誤。 在這種情況下,您應該刪除que2文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM