-1

これは、コマンド ライン引数からの 2 つの文字列を比較し、両方を小文字バージョンに変換し、比較を実行して、辞書順で他の文字列よりも前の文字列または小さい文字列を出力する宿題です。文字列の長さは任意です。

ただし、私のアルゴリズムは再帰を使用する必要があります。

1 - 再帰を使用しているのか、2 - Hello と Hallo では機能しないのに Homework と homePhOne では機能するのかはわかりません。

助けてください!!!

 import java.util.Scanner;


public class MyStringCompare
 {  
 public static void main(String[] args)
  {
Scanner in = new Scanner(System.in);
System.out.println("Input String 1");
String x = in.next();
String xl = toLowerCase(x);
System.out.println("Input String 2");
String y = in.next();
String yl = toLowerCase(y);


char a = xl.charAt(0);
char b = yl.charAt(0);
int min = 0;

if (a < b)
{

  System.out.println("The smallest string is " +xl);
}
if (a > b)
{

  System.out.println("The smallest string is " +yl);
}
else if (a == b)
{
  min = toCompare(xl,yl);
  if (min == -1)
  {
  System.out.println("The smallest string is " +xl);
  }
  if (min == 1)
        {
    System.out.println("The smallest string is " +yl);
  }
  else if (min == 0)
  {
    System.out.println("Two strings are equal");
  }
}



}



 public static String toLowerCase( String s )
{
   String output;
   output = "";
   int i;
   char a;
   char b;


   for (i = 0; i<s.length(); i++)
   {
     a = s.charAt(i);


     if (a >= 65 && a <= 90)
     {
         b = (char)( a+32);
        output = output + b; 
        }
        else
        {

                output = output + a; 

        }
    }

   return output;
   }


    public static int toCompare (String m, String n)
    {
   int i = 0;
    int j = 0;
    int min = 0;
 for (i = 0; i< m.length(); i++)
 {

   for (j = 0; j < n.length(); j++)
   {
     char a = m.charAt(i);

     char b = n.charAt(j);


     if (a < b)
     {
       min = -1;

     }
     if (a > b)
     {
       min = 1; 

     }
     else if (a == b)
     {

       min = 0;

     }

     }
   }





return min;
}






 }
4

2 に答える 2

0

クラス宣言で静的ブール変数 flag = false を定義し、すべての文字列を静的グローバル変数として定義して、それらを再帰メソッドに渡す必要がないようにします。

main から compare(0) を呼び出して、この再帰手順を使用します。

private static void compare(int i)
{  
   if(x1.length()!=x2.length())
   {
       flag =false; return;
   }
   else
   {
   if((i<x1.length())&&(i<x2.length()))
  {
   if(x1.charAt(i)==x2.charAt(i))
   {
    i++;compare(i);flag =true;
   }
   else
   flag =false; return;
  }}
}

フラグが true かどうかをチェックして、文字列が各文字で等しいかどうかを確認します。これは、再帰を使用した私のバージョンのプログラムです。

import java.util.*;

public class sol {

static String x1;
static String x2;
static boolean flag=false;
public static void main(String args[])
{
    Scanner in = new Scanner(System.in);
    x1= (in.nextLine());
    x1=x1.toLowerCase();
    x2= (in.nextLine());
    x2=x2.toLowerCase();
    compare(0);in.close();
    if(flag==true)
        System.out.print("Strings are equal");
    else
        System.out.print("Strings are not equal");
}
private static void compare(int i)
{  
   if(x1.length()!=x2.length())
   {
       flag =false; return;
   }
   else
   {
   if((i<x1.length())&&(i<x2.length()))
  {
   if(x1.charAt(i)==x2.charAt(i))
   {
    i++;compare(i);flag =true;
   }
   else
   flag =false; return;
  }}
}
}
于 2013-04-28T21:36:48.880 に答える
0

再帰的な方法を実行する場合:

  • 必要なパラメータを調べてください:

    -m: 比較する文字列

    -n: 比較する文字列

  • どの基本ケースが必要ですか? これは、メソッドが自分自身を呼び出すのをいつ停止するかを意味します。この場合、比較する文字がなくなったとき。

  • メソッドを再帰的に呼び出す: 左から右に比較してメソッドを呼び出します。例:ハローハロー

    (1)compare hello hallo 
    
     (2)will call-> compare ello allo
    
      (3)which will call-> compare llo llo
    
       (4)which will call-> lo lo
    
        (5)which will call-> o o
    
         (6)which will call-> '' '' 
    
           which wont call more himself: no more characters left to be compared->basecase!
    
  • しかし、戻り値を収集すると、右から左に移動します

         (6)compare '' '' will return true
    
        (5)compare 'o' 'o' will return (the result of 6)true && true ('o'=='o')  ->true
    
       (4)compare 'lo' 'lo' will return (5)true && true ('l'=='l')   ->true
    
      (3)compare 'llo' 'llo' will return (4)true && true ('l' == 'l')   ->true
    
     (2)compare 'ello' 'allo' will return (3)true && false ('e' != 'a')   ->false
    
    (1)compare 'hello' 'hallo' will return (2)false && true ('h' == 'h')   ->false
    
于 2013-04-28T21:54:14.923 に答える