switch-statement

If / Else / Switch returning the wrong results

非 Y 不嫁゛ 提交于 2020-01-07 05:26:08
问题 I'm making a Rock-Paper-Scissors-Lizard-Spock (Big Bang Theory, the tv show) using ReactJS and i'm facing some kind of abstract issue. switch (this.state.playerOnePick === 'Rock') { case((this.state.playerTwoPick === 'Scissors') || (this.state.playerTwoPick === 'Lizard')): return ( <div> <h1>Player One wins !</h1> <h2>P1: {this.state.playerOnePick} P2: {this.state.playerTwoPick}</h2> </div> ); break; case((this.state.playerTwoPick === 'Paper') || (this.state.playerTwoPick === 'Spock')):

Passing a value from one case to another in switch statement

社会主义新天地 提交于 2020-01-07 04:13:04
问题 So here is a sample code : import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("Please type in a number"); Scanner in = new Scanner(System.in); switch (in.nextInt()){ case 1: save(in); break; case 2: System.out.println(value); break; default: System.out.println("Default case"); break; } in.close(); } public static String save(Scanner in){ System.out.println("Type in a word"); String value = in.next(); return value; } } In this particular

Get the name of the requested subobject javascript

别来无恙 提交于 2020-01-06 20:38:01
问题 If I have an object with an anonymous function inside how do I know what subobject is being requested so that I can return a value? var obj = function(a) { switch (a) { case 'subprop1': return 'subval1'; case 'subprop2': return 'subval2'; case 'subprop3': return 'subval3'; default: return 'defaultval'; } } So if I call: obj.subprop1 I should get: subval1 回答1: This is not really possible with plain objects unless your environment supports Proxy. In this case it would be quite easy (haven't

listview toggle switch works only last row

て烟熏妆下的殇ゞ 提交于 2020-01-06 05:23:01
问题 i'm facing a problem which is, when i toggle switch it's works for only last item of listview. It doesn't matter which switch i toggle. I research other asked questions but i couldn't figure it out. @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(getContext()); View customView = inflater.inflate(R.layout.row, parent, false); String allData = getItem(position); final Switch status = (Switch) customView.findViewById

GCC Jump Table initialization code generating movsxd and add?

爱⌒轻易说出口 提交于 2020-01-06 04:12:27
问题 When I compile a switch statement with optimization in GCC, it sets up a jump table like this, (fcn) sym.foo 148 sym.foo (unsigned int arg1); ; arg unsigned int arg1 @ rdi 0x000006e0 83ff06 cmp edi, 6 ; arg1 0x000006e3 0f87a7000000 ja case.default.0x790 0x000006e9 488d156c0100. lea rdx, [0x0000085c] 0x000006f0 89ff mov edi, edi 0x000006f2 4883ec08 sub rsp, 8 0x000006f6 486304ba movsxd rax, dword [rdx + rdi*4] 0x000006fa 4801d0 add rax, rdx ; '(' ;-- switch.0x000006fd: 0x000006fd ffe0 jmp rax

JavaScript switch statement only executes the default case

馋奶兔 提交于 2020-01-05 15:25:11
问题 I'm new to JavaScript and wrote this short script to choose a random background color for the body of my page, but it only keeps executing the default case. I don't know what's the problem. <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <script> function chC(){ var cl=document.getElementById('demo'); var colNo=Math.random()*4; switch (colNo){ case 1:{ cl.style.background='red'; } case 2:{ cl.style.background='yellow'; } case 3:{ cl.style.background='pink'; } default :{ cl

Is there a way for switch to return a string value using C# 8 switch expressions?

我与影子孤独终老i 提交于 2020-01-05 08:27:36
问题 I have this code where each part of the switch returns a value to ModeMessage2 . Is it possible using the new C# switch expressions (or any other code optimization) to optimize the way this switch works? switch (Settings.Mode) { case MO.Learn: ModeMessage2 = "Use this mode when you are first learning the phrases and their meanings."; if (Settings.Cc == CC.H) { Settings.Cc = CC.JLPT5; App.cardSetWithWordCount = null; App.DB.RemoveSelected(); } break; case MO.Practice: ModeMessage2 = "Use this

Using user-inputted characters in If/Switch statements

試著忘記壹切 提交于 2020-01-05 04:20:11
问题 I am taking my first Java class, and am having a hard time finishing this assignment. The assignment itself is quite simple. All I have to do is prompt the user to input an integer, a character, then another integer. If the character inputted is +, the two integers should be added together. If the character inputted is -, the two integers should be subtracted. Etc... The problem is that I do not know how to accept a character from the user, then use it in my If/Switch statements. Here are my

multidimensional index by array of indices in JavaScript

回眸只為那壹抹淺笑 提交于 2020-01-05 04:14:17
问题 Is there a way in JavaScript to select a element of a multidimential array. Where the depth/rank/dimensionality is variable and the keys are given by a array of indices. Such that i don't have handle every possible dimentional depth separately. concretely speaking i want do get rid of switch cases like here: /** * set tensor value by index * @type {array} indices [ index1, index2, index3 ] -> length == rank. * @type {string} value. */ tensor.prototype.setValueByIndex = function( indices,

Does a variable defined inside a switch/case persists it's value? [closed]

瘦欲@ 提交于 2020-01-05 03:31:31
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I know that it is possible to create, but not initalize, a variable inside a switch-statement. But I wonder if a created variable remains in memory, during subsequent executions of the switch-statement? I assume the variable named cnt is newley created every time the switch-statement is executed. Therefore the