import java.io.*;
public class Jia{
public static void main(String[] args) throws IOException{
int i;
float j;
double k=0;
System.out.println("请输入一个数");
j=System.in.read();
if(j>=1)
{
for(i=1;i<=j;i++)
{
k+=1.0/(2*i-1);
}
System.out.println(k);
}
else
System.out.println("please enter a value >= 1");
}
}
你去试试这个
import java.io.*;
public class Jia{
public static void main(String[] args) throws IOException{
int i = 0;
double k=0;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个数");
String text = buf.readLine();
if(Integer.parseInt(text)>=1)
{
for(i=1;i<=Integer.parseInt(text);i++)
{
k+=1.0/(2*i-1);
}
System.out.println(k);
}
else
System.out.println("please enter a value >= 1");
}
}
还可以用这个 我调试过了
import java.io.*;
public class Jia{
public static void main(String[] args) throws IOException{
int i = 0;
double k=0;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个数");
String text = buf.readLine();
if(Integer.parseInt(text)>=1)
{
for(i=1;i<=Integer.parseInt(text);i++)
{
k+=1.0/(2*i-1);
}
System.out.println(k);
}
else
System.out.println("please enter a value >= 1");
}
}
还可以用这个 我调试过了
可以编译
运行结果是:
D:\javawork>javac jia.java
D:\javawork>java jia
Exception in thread "main" java.lang.NoClassDefFoundError: jia (wrong name: Jia)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)