本帖最後由 高鋐鈞 於 2023-11-4 12:06 編輯
104- import java.util.*;
- public class Ch05 {
- public static void main(String args[]) {
- Scanner s=new Scanner(System.in);
- Double x1,x2,y1,y2;
- try{
- x1=s.nextDouble();
- y1=s.nextDouble();
- x2=s.nextDouble();
- y2=s.nextDouble();
- System.out.printf("%.4f",Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
- }catch(Exception e){
- System.out.println("error");
- s.close();
- }
- }
- }
複製代碼 105- import java.util.*;
- public class Ch01 {
- public static void main(String args[]) {
- Scanner s=new Scanner(System.in);
- String dreams = "There are moments in life when you miss someone so much that "
- + "you just want to pick them from your dreams and hug them for real! Dream what "
- + "you want to dream;go where you want to go;be what you want to be,because you have "
- + "only one life and one chance to do all the things you want to do";
- String search=s.nextLine();
- int first,last;
- String capture;
- first=dreams.indexOf(search);
- last=dreams.lastIndexOf(search);
- if(first==-1){
- first=0;
- last=0;
- capture="";
- }else if(first==last){
- last=0;
- capture=dreams.substring(first);
- first++;
- }else {
- capture=dreams.substring(first, last+search.length());
- first++;
- last++;
- }
- System.out.println("first:"+first);
- System.out.println("last:"+last);
- System.out.println("capture:"+capture);
- }
- }
複製代碼 106- import java.util.*;
- public class Ch02 {
- public static void main(String args[]) {
- Scanner s=new Scanner(System.in);
- int a,b,c,d;
- try{
- a=s.nextInt();
- if(a<0)
- a=0;
- }catch(Exception e){
- a=0;
- s.next();
- }try{
- b=s.nextInt();
- if(b<0)
- b=0;
- }catch(Exception e){
- b=0;
- s.next();
- }try{
- c=s.nextInt();
- if(c<0)
- c=0;
- }catch(Exception e){
- c=0;
- s.next();
- }try{
- d=s.nextInt();
- if(d<0)
- d=0;
- }catch(Exception e){
- d=0;
- }
- int tep1,tep2;
- tep1=Math.min(a, b);
- tep1=Math.min(tep1, c);
- tep1=Math.min(tep1, d);
- tep2=Math.max(a, b);
- tep2=Math.max(tep2, c);
- tep2=Math.max(tep2, d);
- System.out.println("smallest:"+tep1);
- System.out.println("largest:"+tep2);
-
- }
- }
複製代碼 202- import java.util.*;
- public class Ch03 {
- public static void main(String args[]) {
- Scanner s=new Scanner(System.in);
- int a;
- try{
- a=s.nextInt();
- }catch(Exception e){
- System.out.println("error");
- return;
- }
- if(a%2==0){
- System.out.println(a+" is an even number.");
- }else{
- System.out.println(a+" is an odd number.");
- }
- }
- }
複製代碼 205- import java.util.Scanner;
- public class Ch04 {
- public static void main(String args[]) {
- Scanner s=new Scanner(System.in);
- int a,b;
- try{
- a=s.nextInt();
- b=s.nextInt();
- if(a<0||a>100||b<0||b>100){
- System.out.println("error");
- return;
- }
- }catch(Exception e){
- System.out.println("error");
- return;
- }
- int x=0;
- for(int i=1;i<=Math.max(a, b);i++){
- if(a%i==0&&b%i==0){
- x=i;
- }
- }
- System.out.print(x);
- }
- }
複製代碼 |