循环和数组格式 [英] Loop and Array format

查看:106
本文介绍了循环和数组格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是完成了这个节目,我不得不写上课。该计划应该从用户的ID和日和我的老师得到的输入要求我们把一个循环,这两个部分的情况下,用户没有输入正确的ID /日。我已经写程序到可以取输入,但它不能区分如果输入是有效还是无效,将继续通过该程序的其余部分。此外,在数组末尾它应该打印阵列,我已经做到了。我只是想放旁边阵列的ID(A,B,C,D)。我想对最终的结果有ID的数组中列出向下左侧的4x5的表来排队。如果你们有关于它如何做到这将会是AP preciated任何意见/建议。

 进口java.util.Scanner中;
公共类推销员
{
 公共静态无效的主要(字串[] args)
 {
  扫描仪输入=新的扫描仪(System.in);
  的char [] =名称{'A','B','C','D'};
  的char []天= {'M','T','W','H','F'};
  双[] [] =本周新的双[4] [5];
  INT停止= 0;
  焦炭nameInput;
  焦炭dayInput;
  双成本;
  INT行;
  INT列;  做
  {
        的System.out.println(输入业务员ID为A,B,C或D);
        串nameString = input.nextLine();
        nameInput = nameString.charAt(0);
        行= rowSearch(姓名,nameInput);        的System.out.println(输入一周为M,T,W,H,或F的天);
        串dayString = input.nextLine();
        dayInput = dayString.charAt(0);
        柱= columnSearch(天,dayInput);        的System.out.println(输入销售金额);
        成本= input.nextDouble();
        周[行] [列] =成本;        的System.out.println(有没有更多的数据输入Y更多或N停止?);
        input.nextLine();
        串dataString = input.nextLine();
        焦炭的DataInput = dataString.charAt(0);        如果(DataInput中的=='N')
        {
                停止= -1;
        }
        其他
        {
                停止= 1;
        }
  }
  而(停止> = 0);  的System.out.println(一二W H F);
  为(行= 0;&行LT; week.length;排++)
  {
        为(列= 0;&列LT;周[行]。长度;柱++)
        {
                System.out.printf($%1.2F,周[行] [列]);
        }
     的System.out.println();
  }
 }
 公共静态INT rowSearch(的char []名称,CHAR idInput)
 {
        INT行= 0;
        的for(int i = 0; I< names.length;我++)
        {
                 如果(idInput ==名[I])
                  排= I;
        }
        返回行;
 }
 公共静态INT columnSearch(的char []天,CHAR columnInput)
 {
        INT列= 0;
        的for(int i = 0; I< days.length;我++)
        {
                如果(columnInput ==天[I])
                  列= I;
        }
        返回列;
 }
}


解决方案

通过写一个或多个可以提示用户对一些输入,并且可以验证响应自己的方法开始。直到一个有效的响应给出(或者输入一些退出code)这些方法应继续循环。

例如...

 公共字符串问(字符串提示字符... validResponses){
    串响应=无效;
    串testResponses =将String.valueOf(validResponses).toLowerCase();
    扫描仪输入=新的扫描仪(System.in);
    做{
        的System.out.println(提示);
        响应= input.nextLine();
    }而(testResponses.contains(response.toLowerCase())!);
    返回响应;
}

然后,你可以简单地调用方法,你需要的信息...

 字符串响应=问(输入业务员ID为A,B,C或D,'A','B','C','D');

您会就用的响应,你通常会...

本例使用这需要迅速和有效字符列表的一个方法。你可以容易地写出两种不同的方法处理这些个人请求......

  nameInput = askForSalesMan();
dayInput = askForDay();

但是,归结到你觉得你能最好地处理...

I'm just finishing up this program I had to write for class. The program is supposed to get input from the user for the "ID" and "Day" and my teacher is asking us to put a loop in both those sections in case the user doesn't input the correct ID/Day. I've written the program to where it can take the input, but it won't be able to differentiate if the input is valid or not and will continue through the rest of the program. Also at the end of the array it's supposed to print the array and I've done that. I just would like to put the ID (A, B, C ,D) next to the array. I'd like for the end result to have the ID to the left of the array listed downwards to line up with the 4x5 table. If you guys have any advice/tips on how it could be done it'd be appreciated.

import java.util.Scanner;
public class Salesman
{
 public static void main(String[]args)
 {
  Scanner input = new Scanner(System.in);
  char[] names = {'A', 'B', 'C', 'D'};
  char[] days = {'M', 'T', 'W', 'H', 'F'};
  double[][] week = new double[4][5];
  int stop = 0;
  char nameInput;
  char dayInput;
  double cost;
  int row;
  int column;

  do
  {
        System.out.println("Enter the salesman ID as A, B, C or D.");
        String nameString = input.nextLine();
        nameInput = nameString.charAt(0);
        row = rowSearch(names, nameInput);

        System.out.println("Enter the day of the week as M, T, W, H, or F");
        String dayString = input.nextLine();
        dayInput = dayString.charAt(0);
        column = columnSearch(days, dayInput);

        System.out.println("Enter the amount of the sale");
        cost = input.nextDouble();
        week[row][column] = cost;

        System.out.println("Is there more data? Enter Y for more or N to stop");
        input.nextLine();
        String dataString = input.nextLine();
        char dataInput = dataString.charAt(0);

        if (dataInput == 'N')
        {
                stop = -1;
        }
        else
        {
                stop = 1;
        }
  }
  while (stop >= 0);

  System.out.println("  M     T     W     H     F");
  for (row = 0; row < week.length; row++)
  {
        for (column = 0; column < week[row].length; column++)
        {
                System.out.printf("$%1.2f ", week[row][column]);
        }
     System.out.println();
  }
 }
 public static int rowSearch(char[] names, char idInput)
 {
        int row = 0;
        for (int i = 0; i < names.length; i++)
        {
                 if (idInput == names[i])
                  row = i;
        }
        return row;
 }
 public static int columnSearch(char[] days, char columnInput)
 {
        int column = 0;
        for (int i = 0; i < days.length; i++)
        {
                if (columnInput == days[i])
                  column = i;
        }
        return column;
 }
}

解决方案

Start by writing one or methods that can prompt a user for some input and that can validate the response themselves. These methods should continue to loop until a valid response is given (or some exit code is entered).

For example...

public String ask(String prompt, char... validResponses) {
    String response = null;
    String testResponses = String.valueOf(validResponses).toLowerCase();
    Scanner input = new Scanner(System.in);
    do {
        System.out.println(prompt);
        response = input.nextLine();
    } while (!testResponses.contains(response.toLowerCase()));
    return response;
}

Then you can simply call the method with the information you need...

 String response = ask("Enter the salesman ID as A, B, C or D.", 'A', 'B', 'C', 'D');

You would then just use the response as you normally would...

This example uses a single method which takes a prompt and a list of valid characters. You could as easily write two separate methods which process the individual requests...

nameInput = askForSalesMan();
dayInput = askForDay();

But that comes down to what you feel you can best handle...

这篇关于循环和数组格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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