如何根据用户输入在哈希表中添加某些值? [英] How do you add up certain values in hashtable based off of user input?

查看:66
本文介绍了如何根据用户输入在哈希表中添加某些值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java创建 gematria 计算器,并尝试获得相同的结果结果我得到与python.我怎么能在java中从用户输入中打印出值的总和,并获得与python中相同的最终结果?请注意,我是Java的初学者,请原谅我对这个问题的任何不了解.

I am trying to make a gematria calculator in Java and to try to get the same results I am getting with python. How would I get so in java it could print the sum of the values from what the user inputs and get the same ultimate result as I would in python? Please note I'm a beginner at java so forgive me for any ignorance in this question.

我设法用python制作了一个,想在Java中做一些概念上相似的事情.在python中,我有一本包含所有英文字母及其数字值的字典.

I managed to make one in python and want to do something conceptually similar in java. In python, I had a dictionary with all the English letters and their numeric values.

Python代码

    Letter_values = {" ":0, 
    `"A": 1,`   
    "B": 2,
    # goes through rest of alphabet.}
    New_total = []  
    def get_gematria():  
          word = input("What do you want the gematria of?")
          upper_word = [x.upper() for x in word]
          for i in upper_word:
          New_total.append(Gematria[i])
          print(F"The gematria of {word} is {sum(New_total)}.")
          New_total.clear
          get_gematria() 
    get_gematria() 

根据我对Java的理解,我会做一个哈希表,然后我将其写出来.

According to my understanding in Java, I would be doing a hashtable and I get how to write that out.

Java代码

    package com.company;
    import java.util.*;
    public class Main {

           public static void main(String[] args)  {
             Hashtable<String, Integer> Letter_Values = new Hashtable<>();
             Letter_Values.put("A", 1);
             Letter_Values.put("B", 2);
            // Goes through rest of alphabet
             System.out.println("What do you want the gematria of?")
             Scanner i = new Scanner(System.in);
             String Gematria = i.next();
             System.out.println("The gematria of " + Gematria + " is " 
               + Letter_Values.get(Gematria))

因此在Java程序中,如果输入是输出的"A" (不带引号),我将得到:

So in the Java program if the input is "A" (without the quotes) for output I'll get:

"The gematria of A is 1."

对于输入,我将输入"B" (不带引号)以得到输出:

and for input I put in "B" (without the quotes) for output I'll get:

"The gematria of B is 2."

但是,如果输入 AB AA 作为输入,则输出将为:

However, if enter AB or AA as input, the output will be:

"The gematria of AB is null."

我还尝试在此处放置 for 循环:

I also tried putting a for loop right here:

for (int j = 0; j <Gematria.length(); j++) {
    System.out.println("The gematria of " + Gematria + " is " 
      + Gematria_Values.get(Gematria));

再次,如果我输入 A 作为输入,我将得到与以前相同的东西:

So again if I put A for input I get the same thing as before:

"The gematria of A is 1."

但是,如果我输入 AA 或我收到的字母的任意组合.

However, if I put AA or any combination of letters I get.

"The gematria of AA is null."
"The gematria of AA is null."

我知道

"The gematria of x is null."

根据字母数,我输入.因此,如果我输入 AAAA .输出:

According to the number of letters, I put in. So if I put in AAAA. Output:

"The gematria of AAAA is null." 4 Times

我认为这很清楚我的Java代码能带来什么.

I think it made it clear what I get with my java code.

在python程序中,如果我输入 aa ab ,我会得到:

In the python program if I put in in aa or ab I get:

"The gematria of aa is 2."

"The gematria of ab is 3."

因此最好将用户输入转换为大写,但这不是我要问的主要内容.

So it would be nice to convert the user input to uppercase but that's not the main thing I'm asking here.

我的主要问题是:如何在Java中获得与在python中相同的最终结果?

My main question is: How do I get the same ultimate result in java as I do in python?

推荐答案

从以下版本更新您的for循环

Update your for loop from

for (int j = 0; j <Gematria.length(); j++) {
    System.out.println("The gematria of " + Gematria + " is " + Gematria_Values.get(Gematria));

int sum = 0;
for (int j = 0; j < Gematria.length(); j++) {
    sum+= Letter_Values.get(String.valueOf(Gematria.charAt(j)));
}
System.out.println(sum);

这篇关于如何根据用户输入在哈希表中添加某些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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