简体   繁体   English

我如何让我的代码计算正确位置值的数量?

[英]how do i get my code to calculate the number of place correct place values?

I am not really good at coding and I've been confused in this step where I don't know to make my code read the correct number of place values.我不太擅长编码,在这一步中我很困惑,我不知道让我的代码读取正确数量的位置值。

For example I run my code and it generates 5 random numbers then I input 5 random numbers the output of this would be "They share 0 numbers in the right place value.例如,我运行我的代码并生成 5 个随机数,然后我输入 5 个随机数,其输出将是“他们在正确的位置值中共享 0 个数字。

For example,in order for me to get what I want, I get 5 random generated numbers which are 54123, I input 00123 it would say they hold 3 numbers in the right place value.例如,为了让我得到我想要的,我得到 5 个随机生成的数字,它们是 54123,我输入 00123 它会说它们在正确的位置值中保存了 3 个数字。

//this is what I think something needs to be edited //This is my whole code on a google doc: https://docs.google.com/document/d/1ug1rRNwgZwBAf9kvaeAEFoo81r_W9Br6rRX4xc0fFUk/edit?usp=sharing //I didn't enter my whole code on this post because I think it is to long and the only part I want is where it can calculate how many numbers in the right place value //这是我认为需要编辑的东西 //这是我在谷歌文档上的全部代码: https : //docs.google.com/document/d/1ug1rRNwgZwBAf9kvaeAEFoo81r_W9Br6rRX4xc0fFUk/edit? usp =sharing //我没有'不要在这篇文章中输入我的全部代码,因为我认为它太长了,我唯一想要的部分是它可以计算正确位置值中有多少个数字

    System.out.println(s);
    String f = scan.next();

    for(int p=0; p<s.length(); p++) {
        for(int z=0;z<f.length();z++){   
            if (s.substring(p,p+1).equals(f.substring(z,z+1))){
                n++;
            } }}
    int rt= 0;
    for(int p=0; p<s.length(); p++) {
        for(int z=0;z<f.length();z++){

            // this where i count the numbers in the right place
            rt=99;
            } }
    System.out.println(" They share " + rt + " numbers in the right place value");
    System.out.println(" They share " + n + " numbers in common");


    System.out.println("guess number 1");
            if(n ==5){
                System.out.println(" Congratulations you win! ");
                System.exit(0);

            }
    String generatedNums = "54123";
    String nums = "00123";

    int length = generatedNums.length() <= nums.length() ? generatedNums.length() : nums.length();
    int count = 0;

    for (int i = 0; i < length; i++) {
        if (generatedNums.charAt(i) == nums.charAt(i)) {
            count++;
        }
    }
    System.out.println(count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM