【问题标题】:I can't make this program with do-while variable, just with for variable我不能用 do-while 变量制作这个程序,只能用 for 变量
【发布时间】:2017-03-26 13:21:54
【问题描述】:

我在程序中使用了变量,但我无法使用 do-while 变量制作这个程序。 (备注:我在 NetBeans 中做的,我是匈牙利人。我在程序中使用了匈牙利语文本。对不起。)

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package karakterkeresés.fr;
import java.util.Scanner;
/**
 *
 * @author Apa---2016
 */
public class KarakterkeresésFr {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
Scanner beolv;
beolv=new Scanner(System.in);
int y=0;
String x;
System.out.print("Megadott szó:");
x=beolv.nextLine();
for(int cv=0;cv<x.length();cv++){
if(x.charAt(cv)=='a'){y++;}
else{}
}
if(y>0){System.out.println("A megadott szóban megtatlálható az 'a' karakter.");}
else{System.out.println("A megadott szóban nem megtatlálható az 'a' karakter.");}
System.out.println("A szóban "+y+" 'a' betű található.");
}
}

这是有效的。 但这不是。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package karakterkeresés.dw;
import java.util.Scanner;
/**
 *
 * @author Apa---2016
 */
public class KarakterkeresésDw {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
Scanner beolv;
beolv=new Scanner(System.in);
int y=0;
int z=0;
String x;
String Sg;
System.out.print("Megadott szó:");
x=beolv.nextLine();
do{
if(x.charAt(0+1)=='a'){z++;}
else{}
y++;
}
while(y<=x.length());
if(z>0){System.out.println("A megadott szóban megtalálható az 'a' betű.");}
else{System.out.println("A megadott szóban nem található meg az 'a' betű.");}
System.out.println("A megadott szóban "+z+" 'a' betű található.");
}
}

如何用do-while变量制作这个程序?

【问题讨论】:

  • 你也可以省略 do 块中的 else 语句,因为它没有做任何事情......
  • 请修正您的代码格式,仅此一项就可以回答/阐明您的问题。
  • 请编辑您的标题和问题,因为 do-while 是一个循环,与 for 循环不同,请澄清您的问题

标签: java string character


【解决方案1】:

也许你的意思

if(x.charAt(y)=='a')
{
  z++;
}

【讨论】:

  • 也在你的 while 中不要在你的测试条件上添加“=”,因为如果你在你的 for 循环中比较它,如果你认为我的答案是正确的,它就没有“=”字符,你可以接受我的回答我的勾选检查这也可能导致 charAt() 方法出错
  • 好的。明天在程序中编写测试。
【解决方案2】:

您需要使用 charAt(y) 检查每个索引处的字符,并且您不需要不必要的 else 条件(什么都不做)

 do {
    if (x.charAt(y) == 'a') {
        z++;
    } 
    y++;            
  } while (y < x.length());

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多