Advanced Programming-Week7
Summary
- 字符串
- 拼接
- charAt()
Task50
package Task50;
import javax.swing.*;
import java.util.*;
public class Task50{
public static void main(String []args)
{
int a;
Scanner in = new Scanner(System.in);
a=in.nextInt();
if(a<1||a>9)
{
System.out.println("The number you input is error.");
}else {
for(int i=0;i<a;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print(j+1);
}
System.out.println();
}
System.out.println();
for(int i=a;i>0;i--)
{
int t=a;
for(int j=0;j<i;j++)
{
System.out.print(t--);
}
System.out.println();
}
}
}
}
Task51
package Task51;
import java.util.Scanner;
public class Task51 {
public static void main(String []args)
{
int rows,columns;
char use;
Scanner in = new Scanner(System.in);
System.out.printf("Enter number of rows:\t\t");
rows=in.nextInt();
if(rows<1||rows>25)
{
System.out.printf("The rows you enter is not a right number.");
}
System.out.printf("Enter number of columns:\t");
columns=in.nextInt();
if(columns<1||columns>25)
{
System.out.printf("The columns you enter is not a right number.");
}
System.out.printf("Enter symbol of use:\t\t");
in.nextLine();
String t=in.nextLine();
use=t.charAt(0);
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
System.out.print(use);
}
System.out.println();
}
}
}
Task52
import java.util.*;
public class Task52{
public static void main(String []args)
{
for(int i=1;i<=100;i++)
{
String ans=""+i;
if(i%3==0)
{
ans="Fizz";
}
if(i%5==0)
{
if(ans.equals("Fizz"))
ans+="Buzz";
else ans="Buzz";
}
if(i%5!=0)
System.out.printf(ans+"\t");
else
System.out.println(ans+"\t");
}
}
}
Task53
package Task53;
import java.util.Scanner;
public class Task53 {
public static void main(String []args)
{
int n;
Scanner in = new Scanner(System.in);
n=in.nextInt();
int n1=n;
int n2=0;
while(n!=0)
{
n2*=10;
n2+=n%10;
n/=10;
}
if(n1==n2){
System.out.println("The number is a palindromic number.");
}else
System.out.println("The number is not a palindromic number.");
}
}
Task54
package Task54;
import java.util.*;
import javax.swing.*;
public class Task54 {
public static void main(String []args)
{
String menuMessage;
double pi = 3.14,sideLength,base,height,area;
menuMessage = "Choose an option from the following menu:";
menuMessage += "\n1. Calculate area of circle";
menuMessage += "\n2. Calculate area of square";
menuMessage += "\n3. Calculate area of triangle";
String option = JOptionPane . showInputDialog (null , menuMessage) ;
System.out.println(option);
if (option.equals("1"))
{
double radius = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius:"));
area = pi*radius*radius;
JOptionPane.showMessageDialog(null, "The area is" + area) ;
}
else if (option.equals("2"))
{
sideLength = Double.parseDouble (JOptionPane . showInputDialog (null, "Enter length of a side"));
area = sideLength*sideLength;
JOptionPane.showMessageDialog (null,"The area is"+ area) ;
}
else if (option.equals("3"))
{
base = Double.parseDouble (JOptionPane .showInputDialog(null, "Enter base length"));
height = Double.parseDouble (JOptionPane .showInputDialog(null, "Enter height length"));
area = base*height*0.5;
JOptionPane.showMessageDialog(null, "The area is"+ area) ;
}
else
JOptionPane.showMessageDialog(null, "Hit any key to try again");
}
}
Task55
package Task55;
import java.util.Random;
public class Task55 {
public static void main(String []args)
{
Random random = new Random();
int []a = new int[7];
for(int i=0;i<600;i++)
{
int f=random.nextInt(6)+1;
a[f]++;
}
System.out.println("Face\tFrequency");
for(int i=0;i<6;i++){
System.out.println(i+1+"\t"+a[i+1]);
}
}
}
Task56
package Task56;
public class Task56 {
public static void main(String []args)
{
int salary1=0;
int salary2=0;
for(int i=1;i<=10;i++)
{
salary1+=100;
}
int basic=1;
for(int i=1;i<=10;i++)
{
salary2+=basic;
basic*=2;
}
System.out.println("The option1 is "+ salary1);
System.out.println("The option1 is "+ salary2);
}
}
Task58
package Task58;
import java.util.*;
public class Task58 {
public static void main(String []args)
{
int a;
Scanner in= new Scanner(System.in);
a=in.nextInt();
if(a<5||a>9)
{
System.out.println("The range you enter is error.");
}else{
for(int i=0;i<a;i++)
{
for(int j=a;j>i;j--)
{
System.out.print(j);
}
System.out.println();
}
System.out.println();
for(int i=1;i<=a;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
}
Task59
package Task59;
import java.util.*;
import javax.swing.JOptionPane;
public class Task59 {
public static void main(String []args)
{
int []a=new int[7];
int num;
num=Integer.parseInt(JOptionPane.showInputDialog(null,"How many random notes would you like \n(input must be in the range 1 to 50)?"));
Random random=new Random();
for(int i=0;i<num;i++)
{
int t;
t=random.nextInt(7);
a[t]++;
}
String message="Count of each note is as follows:";
for(int i=0;i<7;i++){
message+="\n"+(char)(i+'A')+":"+a[i];
}
JOptionPane.showMessageDialog(null,message);
}
}