简体   繁体   English

'。类。' java中的错误

[英]'.class.' error in java

I tried a problem which worked fine on C++; 我尝试了一个在C ++上运行良好的问题; here is the C++ implementation. 这是C ++实现。

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
int t;
char str[1000100];
char num[1010];
cin>>t;
while(t--)
{
    cin>>num;
    cin>>str;
    int a=strlen(str);
    int b=strlen(num);
    int x=0;
    int flag=0;
    int m=0;
    for(int j=0;j<2*b;j++)
    {
        if(m==b)
        {
        flag=1;
        m--;
        }
        if(m==0)
        {
        flag=0;

        }



        num[j]=num[m];
        if(flag==0)
        m++;
        if(flag==1)
        m--;
    }
    for(int i=0;i<a;i++)
    {
        if(x==2*b)
            x=0;
        if((str[i]-(num[x]-'0'))<97)
        str[i]=char(str[i]-(num[x]-'0')+26);
         else
         str[i]=char(str[i]-(num[x]-'0'));
        x++;



     }
     cout<<str<<endl;
    //cout<<num;
}
return 0;
  }

Here is my Java implementation. 这是我的Java实现。

import java.util.Scanner;
class sngmsg
{
public static void main(String[] s)
{
    Scanner sc=new Scanner(System.in);
    int t;
    /*char[] str;
    str = new char[1000100];
    char[] num=new char[1010];*/
    String str;
    String num;
    char[] num1=new char[1010];
    char[] str1=new char[1000100];
    t=sc.nextInt();
    for(int l=0;l<t;l++)
    {

        num=sc.next();
        str=sc.nextLine();
        int a=str.length();
        for(int c=0;c<a;c++)
            str1[c]=str.charAt(c);
    int b=num.length();
    int x=0;
    int flag=0;
    int m=0;
    for(int j=0;j<2*b;j++)
    {
        if(m==b)
        {
        flag=1;
        m--;
        }
        if(m==0)
        {
        flag=0;

        }



        num1[j]=num.charAt(m);
        if(flag==0)
        m++;
        if(flag==1)
        m--;
    }
    for(int i=0;i<a;i++)
    {
        if(x==2*b)
        x=0;
        if(int(str1[i])-(num1[x])<97)
        str1[i]=char(int(str1[i])-num1[x])+26);
        else
        str1[i]=char(str1[i]-(num1[x]-'0'));
        x++;



    }

    }
}
}

In the section 在该部分

if(int(str1[i])-(num1[x])<97)
str1[i]=char(int(str1[i])-num1[x])+26);
else
str1[i]=char(str1[i]-(num1[x]-'0'));
x++;

it is giving an error: 它给出了一个错误:

unexpected type
required:value
found: class
'.class' expected
; expected

and similar errors in these lines. 以及这些行中的类似错误。 Any help? 有帮助吗?

it should be 它应该是

if((int)(str1[i])-(int)(num1[x])<97)

cast operator should have () over them like (int) 1 and (char) 'a' instead of int(1) or char('a') cast运算符应该对它们有(),如(int)1和(char)'a'而不是int(1)或char('a')

int and char aren't functions, but are primitives. intchar不是函数,而是原语。

str1[i]=char(int(str1[i])-num1[x])+26)

You're trying to use int as a function. 您正在尝试将int用作函数。 What do you expect from it? 你对它有什么期望?

Usualy, you will have a public class with your main method. 通常,您将使用主要方法进行公开课。 It seems that you are compiling your class and you are not able to execute it. 您似乎正在编译您的类,但您无法执行它。 Do this to solve your problem: 这样做可以解决您的问题:

1 - Create a file with your class name, example "MyClass.java" 1 - 使用您的类名创建一个文件,例如“MyClass.java”

2 - Your class declaration needs to be public: 2 - 您的班级声明需要公开:

public class MyClass {
    // your code here
}

3 - Place your main method inside the created class: 3 - 将main方法放在创建的类中:

public class MyClass {
    public static void main( String[] args ) {
        // your code here
    }
}

4 - Compile it (javac MyClass.java) 4 - 编译它(javac MyClass.java)

5 - Execute it: java MyClass 5 - 执行它:java MyClass

Your code seems to have some problems too. 你的代码似乎也有一些问题。 If you want to cast some primitive or reference type, you need to put the desired type inside parenthesis. 如果要转换某些基本类型或引用类型,则需要将所需类型放在括号内。 Something like: 就像是:

// a char
char c = 'a';

// casting that char to an integer
int i = (int) c;

Let me know if it worked. 让我知道它是否有效。

You are missing a package name at the start of the file. 您在文件的开头缺少包名称。 Also the class should be public. 这堂课也应该是公开的。

package something.somethingelse;
import java.util.Scanner;
public class sngmsg{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM