请让我知道该方法的哪些部分未正确编码,如果可能,请提供相关提示进行更正。 [英] Please let me know which parts of the method is not coded correctly, if possible with hints to correct them ..

查看:73
本文介绍了请让我知道该方法的哪些部分未正确编码,如果可能,请提供相关提示进行更正。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图纠正它,请告诉我,如果我在编写我的方法时一般是正确的方向,它的逻辑是否正确,然后哪些部分需要重写并提示他们。在这种情况下,我可以更好地理解



  public   class  check {
public static void main( String [] args){
String a = abc d;
字符串 b = abcd fe< /跨度>;

System.out.println(compareString(a,b));

}

public static boolean compareString( String a, String b){
// 这里我尝试声明数组大小我不知道我是否可以这样做?
字符串 c [] = new 字符串 [a.length];
字符串 d [] = new String [b.length];
int i = 0;
int j = 0;
// 这里我尝试拆分它们并将每个char存储在数组中
for (i = 0; i< = a.length; i ++){
String c [i] = a.split [ ];
}
for (j = 0; j< = b.length; b ++){
字符串 d [j] = b.split()[ ] ;
}

// 这里我试图计算匹配并加上它们加上计算单词的数量
int matches = 0;
int numberOfWords = 0;
for (i = 0; i< = c.length(); i ++){
for (j = 0; j< = d.length; j ++){
if (c [i] == d [j] ){
匹配++;
}
numberOfWords ++;
}
}
// 这样写它是否正确?
if (匹配/ numberOfwords * 100> 0。 9 * 100 ){
return true;
{
return fales;
}
}

}
}







 //我试图修复错误,但它给出了另一个错误.. 
找到2个错误:
文件:C:\ Users \ Acer \Desktop\check.java [line:17]
错误:令牌i上的语法错误,删除此令牌
文件:C:\ Users \Acer \Desktop\check .java [line:20]
错误:令牌j上的语法错误,删除此令牌













...



写一个方法比较字符串(java),它接受两个字符串输入并返回一个布尔值,表示你的程序是否断定这两个字符串彼此非常相似。布尔结果为true表示字符串彼此非常接近,而false表示不相反。您的方法应确定这是基于以下规则:



如果第一个字符串中至少90%的单词出现在第二个字符串中且至少90%第二个String中的单词出现在第一个String中,您的方法应该返回true。否则你的方法应该返回false。区分大小写,(你好!=你好)



要在编写方法时测试你的方法,我们可以编写一个main方法,所以从main方法调用方法输入然后打印结果。代码中没有任何地方我们应该使用Scanner class.belwo是我尝试的:



我试图声明一个字符串[],并通过使用split方法存储chcars然后比较它们但我不知道如何比较90%我比较整个arrys ..



谢谢



我尝试过:



引用:

公共类检查{

public static void main(String [] args){

String a =abc d;

String b =abcd fe;



System.out.println(compareString(a,b));

< br $>
}



public static boolean compareString(String a,String b){

//这里我尝试声明数组大小我不知道我是否可以这样做?

String c [] = new String [a.length];

String d [] = new String [b.length];

int i = 0;

int j = 0;

//这里我尝试拆分它们存储每个数组中的char

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

String c [i] = a.split [];

}

for (j = 0; j< = b.length; b ++){

String d [j] = b.split()[];

}



//在这里我试图计算匹配并将它们加起来加上数字的数字

int matches = 0;

int numberOfWords = 0;

for(i = 0; i< = c.length(); i ++){

for(j = 0; j< = d.length; j ++){

if(c [i] == d [j]){

匹配++;

}

numberOfWords ++;

}

}

//这是这样写的吗?

if(match / numberOfwords * 100> 0.9 * 100){

返回true;

{

返回fales;

}

}



}

}





//我试图修复错误,但它给出了另一个错误..

找到2个错误:

文件:C:\Users\Acer \Desktop\check.java [line:17]

错误:令牌i上的语法错误,删除此令牌

文件:C:\Users\Acer \Desktop\check.java [line:20]

错误:令牌j上的语法错误,删除此令牌

















  import  java.util.array; 
public class check {
public static void main( String [] args){
String a = aaaaa aaa;
字符串 b = bbbbbbaa aa;

Sysytem.out.println(compareString(a,b);
}
}
public sttaic boolean compareString(string a, String b){
字符串 c [] = new 字符串 [a.length];
字符串 d [] = new 字符串 [b.length];

for (i = 0; i< = a.length; i ++ 0){
字符串 c [i] = a.split [ ];
}
for (j = 0; j< = b.length; b ++){
字符串 d [j] = b.split([ ]);
}

if (Arrays.equals(c,d); {
返回 true;
}
其他 {
返回 false;
}

解决方案

编译并不代表你的代码是对的!:笑:

将开发过程视为编写电子邮件:成功编译意味着您使用正确的语言编写电子邮件 - 例如英语而不是德语 - 而不是电子邮件中包含您要发送的邮件。 />


所以现在你进入第二阶段的开发(实际上它是第四或第五阶段,但你将在之后的阶段进入):测试和调试。 />


首先看看它做了什么,以及它与你想要的有什么不同。这很重要,因为它可以为你提供有关它为什么这样做的信息。例如,如果一个程序是为了让用户输入一个数字并将其翻倍并打印出答案,那么如果输入/输出是这样的:

输入预期输出实际产量
1 2 1
2 4 4
3 6 9
4 8 16

然后很明显问题是双倍的问题它 - 它不是自己添加自己,或者将它乘以2,它将它自身相乘并返回输入的平方。

所以,你可以看一下代码,很明显它在某处:

  private   int   Double  int   value 
{
return * value ;
}



一旦你知道可能出现的问题,就开始使用调试器找出原因。在您的行上设置一个断点:

 字符串 d [] =  new   String  [b.length]; 

并运行您的应用。在执行代码之前,请考虑代码中的每一行应该做什么,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。

如果没有,为什么不呢?它有何不同?



这是一项非常值得开发的技能,因为它可以帮助你在现实世界和发展中。和所有技能一样,它只能通过使用来改进!


作为一名程序员,也需要仔细阅读要求:

引用:

如果第一个字符串中至少有90%的单词出现在第二个字符串中,并且第二个字符串中至少90%的单词出现在第一个字符串中,你的方法应该返回true。



引用:

但我不知道如何比较90 %



您不比较90%,您逐个比较每个单词并计算匹配。

匹配/单词数是您的百分比。


I tried to correct it please let me know if I'm in the right direction in writing my method in general, is its logic correct , and then which parts need to be rewritten and hints for them. in this sway I can understand better

public class check{
public static void main(String[] args){
String a = "abc d";
String b= "abcd fe";

System.out.println(compareString(a,b));

}

public static boolean compareString(String a, String b){
// here i try to declare array size i dont know if i can do it in this way ?
String c[]= new String [a.length];
String d[]= new String [b.length];
int i=0;
int j=0;
// here i try to split them and store each char in arrays
for ( i=0; i<=a.length;i++){
String c[i] =a.split[" "];
}
for (j=0;j<=b.length;b++){
String d [j]=b.split()[" "];
}

// here i tried to count matches and add them up plus counting the numer of words 
int matches=0;
int numberOfWords=0;
for (i=0;i<=c.length();i++){
for( j=0;j<=d.length;j++){
if (c[i]==d[j]){
matches++;
}
numberOfWords++;
}
}
// here is it correct to write it this way ? 
if (match/numberOfwords*100>0.9*100){
return true;
{ 
return fales;
}
}

}
}




// I tried to fix the errors but it gives another erros ..
2 errors found:
File: C:\Users\Acer\Desktop\check.java [line: 17]
Error: Syntax error on token "i", delete this token
File: C:\Users\Acer\Desktop\check.java [line: 20]
Error: Syntax error on token "j", delete this token







...

Write a method compare Strings (java) which takes as input two Strings and returns a boolean representing whether your program concludes that the two strings are very similar to each other. A boolean result of true indicates the Strings are very close to each other and false indicates otherwise. Your method should determine this is based on the following rule:

If at least 90% of the words in the first String appear in the second String AND at least 90% of the words in the second String appear in the first String, your method should return true. Otherwise your method should return false. case sensitive,(hello!=HELLO)

To test you method as you write it, we can write a main method so call the method from the main method on various inputs and then print the results. Nowhere in the code should we be using the Scanner class.belwo is what I tried :

I tried to declare a string [] , and by using split method store the chcars and then compare them but I don't know how to compare 90% I compared the whole arrys..

thanks

What I have tried:

Quote:

public class check{
public static void main(String[] args){
String a = "abc d";
String b= "abcd fe";

System.out.println(compareString(a,b));

}

public static boolean compareString(String a, String b){
// here i try to declare array size i dont know if i can do it in this way ?
String c[]= new String [a.length];
String d[]= new String [b.length];
int i=0;
int j=0;
// here i try to split them and store each char in arrays
for ( i=0; i<=a.length;i++){
String c[i] =a.split[" "];
}
for (j=0;j<=b.length;b++){
String d [j]=b.split()[" "];
}

// here i tried to count matches and add them up plus counting the numer of words
int matches=0;
int numberOfWords=0;
for (i=0;i<=c.length();i++){
for( j=0;j<=d.length;j++){
if (c[i]==d[j]){
matches++;
}
numberOfWords++;
}
}
// here is it correct to write it this way ?
if (match/numberOfwords*100>0.9*100){
return true;
{
return fales;
}
}

}
}


// I tried to fix the errors but it gives another erros ..
2 errors found:
File: C:\Users\Acer\Desktop\check.java [line: 17]
Error: Syntax error on token "i", delete this token
File: C:\Users\Acer\Desktop\check.java [line: 20]
Error: Syntax error on token "j", delete this token









import java.util.array;
public class check{
  public static void main(String[] args){
    String a = aaaaa aaa;
    String b= bbbbbbaa aa;
    
    Sysytem.out.println( compareString(a,b);
                        }
}
public sttaic boolean compareString(string a, String b){
  String c[]= new String [a.length];
  String d[]= new String [b.length];
  
  for ( i=0; i<=a.length;i++0){
    String c[i] =a.split[" "];
  }
  for (j=0;j<=b.length;b++){
    String d [j]=b.split([" "]);
  }
  
  if ( Arrays.equals(c,d);{
    return true;
  }
    else{
      return false;
    }

解决方案

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:

Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:

private int Double(int value)
   {
   return value * value;
   }


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:

String d[]= new String [b.length];

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!


Being a programmer is also a matter of reading carefully the requirements:

Quote:

If at least 90% of the words in the first String appear in the second String AND at least 90% of the words in the second String appear in the first String, your method should return true.


Quote:

but I don't know how to compare 90%


You don't compare 90%, you compare every words one by one and count the matches.
matches / number of words is your percentage.


这篇关于请让我知道该方法的哪些部分未正确编码,如果可能,请提供相关提示进行更正。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆