Java for循环在执行之前读取两次
Wen每当我运行这个程序时,for循环总是打印出它里面的消息2次,然后再让我将任何内容分配给它里面的字符串,但是它会增加Java for循环在执行之前读取两次,java,for-loop,Java,For Loop,Wen每当我运行这个程序时,for循环总是打印出它里面的消息2次,然后再让我将任何内容分配给它里面的字符串,但是它会增加I变量 以下是循环: String rua; for(int i = 1 ; i < horizontal ; i++ ){ do{ System.out.println("Introduza os "+ vertical + " valores da rua " + i); rua = stringTest("erro", sc
I
变量
以下是循环:
String rua;
for(int i = 1 ; i < horizontal ; i++ ){
do{
System.out.println("Introduza os "+ vertical + " valores da rua " + i);
rua = stringTest("erro", sc);
} while(rua.length() != vertical);
}
}首先,您必须检查字符串是否仅填充了字符“-”或“.”,并且您没有引用,但还需要检查长度是否正确。 为此,您必须创建方法来验证:(不完整代码)
String rua=verificatamahnosimbolos(sc,垂直);
验证曼希姆博洛斯(扫描仪sc,int垂直){
布尔误差=真;
做{
erro=真;
系统输出打印(“导入操作系统”+垂直+valores da rua”+i);
字符串linha=sc.nextLine();
如果(验证卡塔曼霍(林哈,维特凯斯))错误=错误;
else System.out.println(“Tamanho errado”);
如果(verifycasimbolos(linha))erro=false;
else System.out.println(“Simbolo errado”);
}while(erro);
验证卡塔曼霍(字符串linha,int verticais){
if(linha.length()==verticais)返回true;
否则返回false
}
验证卡辛博洛斯(字符串linha){
布尔误差=真;
for(int c=0;c
提供完整的代码。是的,问题可能出在您的输入法中。stringTest()做什么,什么是“垂直”、“水平”和“sc”?string test只接收用户输入的字符串,并验证它是否仅由“-”和“.”组成我将它们设为null,然后检查字符串大小是否为null。vertical是垂直collams的数量horizontal是水平collams的数量sc是我的扫描者的名字有一些帖子说不要使用do-while,因为它运行两次,即使没有do-while,或者只是a-for,内部有一段时间,它们都运行在给我任何东西之前,我已经检查过两次了。
public class SeriousLand {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Quantas ruas horizontais? (valor entre 2 e 50)");
int horizontal = SeriousLand.lerValorNoIntervalo(2, 50, "Pretendo um inteiro entre 2 e 50", sc);
System.out.println("Quantas ruas verticais? (valor entre 2 e 50)");
int vertical =SeriousLand.lerValorNoIntervalo(2, 50, "Pretendo um inteiro entre 2 e 50", sc);
System.out.println("Qual a rua horizontal do ponto P? (valor entre 1 e "+horizontal+")");
int horiP = SeriousLand.lerValorNoIntervalo(1, horizontal, "Pretendo um inteiro entre 1 e "+ horizontal, sc);
System.out.println("Qual a rua vertical do ponto P? (valor entre 1 e " + vertical + ")");
int veriP =SeriousLand.lerValorNoIntervalo(1, vertical, "Pretendo um inteiro entre 1 e " + vertical, sc);
String rua;
for(int i = 1 ; i < horizontal ; i++ ){
do{
System.out.println("Introduza os "+ vertical + " valores da rua " + i);
rua = stringTest("erro", sc);
}
while(rua.length() != vertical);
}
//linha++;
/*do {
for (int i = 1; i <= horizontal; i++) {
System.out.println("Introduza os "+ vertical + " valores da rua " + i);
val = true;
for (int j = 1; j <= vertical; j++) {
int valor = sc.nextInt();
if (valor != '-' || valor != '.') {
SeriousLand.imprimeErro(vertical, i);
val = false;
}
}
linha++;
}
}while (linha <= horizontal);
*/
}
static String lerSimbolo ( String errMess, Scanner sc ) {
boolean erro = true;
String simbolo = "";
do {
if ( sc.hasNextLine () ) {
simbolo = sc.nextLine (); // consome a String
erro = false;
}
else {
sc.next (); // consome o que lah esteja
System.out.println ( errMess );
}
} while ( erro );
return simbolo;
}
/**
* Ler numero inteiro no canal de leitura
* @param errMess - mensagem a escrever no System.out caso o valor
* acessivel no canal de leitura nao seja um inteiro
* @param sc - canal de leitura
* @return valor inteiro
* @requires errMess != null && sc != null
*/
static int lerInteiro ( String errMess, Scanner sc ) {
int valor = 0;
boolean erro = true;
do {
if ( sc.hasNextInt () ) {
valor = sc.nextInt (); // consome o inteiro
erro = false;
}
else {
sc.next (); // consome o que lah esteja
System.out.println ( errMess );
}
} while ( erro );
return valor;
}
/**
* Ler numero inteiro no canal de leitura que esta num dado intervalo
* @param infLim Limite inferior do intervalo
* @param supLim Limite superior do intervalo
* @param errMess Mensagem de erro a apresentar no System.out
* @param sc Canal de leitura
* @return um valor entre infLim e supLim
* @requires sc != null && infLim <= supLim && errMess != null
*/
static int lerValorNoIntervalo(int infLim, int supLim,
String errMess, Scanner sc) {
int valor = 0;
boolean erro;
do {
valor = lerInteiro ( errMess, sc );
erro = valor < infLim || valor > supLim;
if ( erro )
System.out.println ( errMess );
} while ( erro );
return valor;
}
public static int lerValorDaLinha(String rua, int coluna, Scanner sc,String erroMess){
int conta = rua.length() + 1;
boolean erro;
do {
conta = coluna;
erro = conta < coluna || conta > coluna;
if ( erro )
System.out.println ( erroMess );
} while ( erro );
return conta;
}
public static void imprimeErro(int vertical,int linha) {
System.out.println("Erro nos valores introduzidos! Tente de novo");
System.out.println("Introduza os "+ vertical + " valores da rua " + linha + " : ");
}
public static String stringTest (String errMess, Scanner sc){
String teste = sc.nextLine();
boolean erro = false;
do {
teste.replaceAll("-","");
teste.replaceAll(".","");
if(teste.length() == 0)
erro = true;
else
System.out.println(errMess);
}
while(erro = false);
return teste;
}
String rua = verificaTamanhoSimbolos(sc, vertical);
verificaTamanhoSimbolos(Scanner sc, int vertical) {
boolean erro = true;
do{
erro = true;
System.out.println("Introduza os "+ vertical + " valores da rua " + i);
String linha = sc.nextLine();
if(verificaTamanho(linha, verticais)) erro = false;
else System.out.println("Tamanho errado");
if(verificaSimbolos(linha)) erro = false;
else System.out.println("Simbolo errado");
}while(erro);
verificaTamanho(String linha, int verticais) {
if(linha.length() == verticais) return true;
else return false
}
verificaSimbolos(String linha) {
boolean erro = true;
for (int c = 0; c < linha.length(), c++) {
if (linha.charAt(c) != '-' && linha.charAt(c) != '.') erro = false;
}
return erro;
}