1.写代码实现从控制台接受一个char类型字符,并且输出
import java.io.*;
import java.util.Scanner;
public class total {
public static void main (String args[]) throws IOException{
String mystring;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("input a bufferReader string");
mystring=buf.readLine();
System.out.println("bufferReader string="+mystring);
Scanner scannerstring=new Scanner(System.in);
System.out.println("input a scanner string");
mystring=scannerstring.next();
System.out.println("scanner string="+mystring);
}
}
更正:根据题意将代码调整为:
import java.io.*;
import java.util.Scanner;
public class total {
public static void main (String args[]) throws IOException{
char c = (char)System.in.read();
System.out.println("c="+c);
}
}
2.打印
public class print{
public static void main(String args[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}
3、求1-1/2+1/3-1/4+…+1/99-1/100的和
import java.lang.Math;
public class math{
public static void main(String args[]){
double sum=0;
for(int i=1;i<=100;i++){
sum=sum+Math.pow(-1,i+1)*1/i;
}
System.out.println("1-1/2+1/3+...+1/99-1/100="+sum);
}
}